home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / emacs / 19.22 / lisp / dired.el < prev    next >
Lisp/Scheme  |  1993-11-22  |  90KB  |  2,314 lines

  1. ;;; dired.el --- directory-browsing commands
  2.  
  3. ;; Copyright (C) 1985, 1986, 1992, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>.
  6. ;; Version: 6
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This is a major mode for directory browsing and editing.  It is
  27. ;; documented in the Emacs manual.
  28.  
  29. ;; Rewritten in 1990/1991 to add tree features, file marking and
  30. ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
  31. ;; Finished up by rms in 1992.
  32.  
  33. ;;; Code:
  34.  
  35. ;;; Customizable variables
  36.  
  37. ;;;###autoload
  38. (defvar dired-listing-switches "-al"
  39.   "*Switches passed to `ls' for dired.  MUST contain the `l' option.
  40. May contain all other options that don't contradict `-l';
  41. may contain even `F', `b', `i' and `s'.")
  42.  
  43. ; Don't use absolute paths as /bin should be in any PATH and people
  44. ; may prefer /usr/local/gnu/bin or whatever.  However, chown is
  45. ; usually not in PATH.
  46.  
  47. ;;;###autoload
  48. (defvar dired-chown-program
  49.   (if (memq system-type '(hpux dgux usg-unix-v irix))
  50.       "chown" "/etc/chown")
  51.   "Name of chown command (usually `chown' or `/etc/chown').")
  52.  
  53. ;;;###autoload
  54. (defvar dired-ls-F-marks-symlinks nil
  55.   "*Informs dired about how `ls -lF' marks symbolic links.
  56. Set this to t if `insert-directory-program' with `-lF' marks the symbolic link
  57. itself with a trailing @ (usually the case under Ultrix).
  58.  
  59. Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
  60. nil (the default), if it gives `bar@ -> foo', set it to t.
  61.  
  62. Dired checks if there is really a @ appended.  Thus, if you have a
  63. marking `ls' program on one host and a non-marking on another host, and
  64. don't care about symbolic links which really end in a @, you can
  65. always set this variable to t.")
  66.  
  67. ;;;###autoload
  68. (defvar dired-trivial-filenames "^\\.\\.?$\\|^#"
  69.   "*Regexp of files to skip when finding first file of a directory.
  70. A value of nil means move to the subdir line.
  71. A value of t means move to first file.")
  72.  
  73. ;;;###autoload
  74. (defvar dired-keep-marker-rename t
  75.   ;; Use t as default so that moved files "take their markers with them".
  76.   "*Controls marking of renamed files.
  77. If t, files keep their previous marks when they are renamed.
  78. If a character, renamed files (whether previously marked or not)
  79. are afterward marked with that character.")
  80.  
  81. ;;;###autoload
  82. (defvar dired-keep-marker-copy ?C
  83.   "*Controls marking of copied files.
  84. If t, copied files are marked if and as the corresponding original files were.
  85. If a character, copied files are unconditionally marked with that character.")
  86.  
  87. ;;;###autoload
  88. (defvar dired-keep-marker-hardlink ?H
  89.   "*Controls marking of newly made hard links.
  90. If t, they are marked if and as the files linked to were marked.
  91. If a character, new links are unconditionally marked with that character.")
  92.  
  93. ;;;###autoload
  94. (defvar dired-keep-marker-symlink ?Y
  95.   "*Controls marking of newly made symbolic links.
  96. If t, they are marked if and as the files linked to were marked.
  97. If a character, new links are unconditionally marked with that character.")
  98.  
  99. ;;;###autoload
  100. (defvar dired-dwim-target nil
  101.   "*If non-nil, dired tries to guess a default target directory.
  102. This means: if there is a dired buffer displayed in the next window,
  103. use its current subdir, instead of the current subdir of this dired buffer.
  104.  
  105. The target is used in the prompt for file copy, rename etc.")
  106.  
  107. ;;;###autoload
  108. (defvar dired-copy-preserve-time t
  109.   "*If non-nil, Dired preserves the last-modified time in a file copy.
  110. \(This works on only some systems.)")
  111.  
  112. ;;; Hook variables
  113.  
  114. (defvar dired-load-hook nil
  115.   "Run after loading dired.
  116. You can customize key bindings or load extensions with this.")
  117.  
  118. (defvar dired-mode-hook nil
  119.   "Run at the very end of dired-mode.")
  120.  
  121. (defvar dired-before-readin-hook nil
  122.   "This hook is run before a dired buffer is read in (created or reverted).")
  123.  
  124. (defvar dired-after-readin-hook nil
  125.   "Hook run after each time a file or directory is read by Dired.
  126. After each listing of a file or directory, this hook is run
  127. with the buffer narrowed to the listing.")
  128. ;; Note this can't simply be run inside function `dired-ls' as the hook
  129. ;; functions probably depend on the dired-subdir-alist to be OK.
  130.  
  131. ;;; Internal variables
  132.  
  133. (defvar dired-marker-char ?*        ; the answer is 42
  134.   ;; so that you can write things like
  135.   ;; (let ((dired-marker-char ?X))
  136.   ;;    ;; great code using X markers ...
  137.   ;;    )
  138.   ;; For example, commands operating on two sets of files, A and B.
  139.   ;; Or marking files with digits 0-9.  This could implicate
  140.   ;; concentric sets or an order for the marked files.
  141.   ;; The code depends on dynamic scoping on the marker char.
  142.   "In Dired, the current mark character.
  143. This is what the `do' commands look for and what the `mark' commands store.")
  144.  
  145. (defvar dired-del-marker ?D
  146.   "Character used to flag files for deletion.")
  147.  
  148. (defvar dired-shrink-to-fit
  149.   t
  150. ;; I see no reason ever to make this nil -- rms.
  151. ;;  (> baud-rate search-slow-speed)
  152.   "Non-nil means Dired shrinks the display buffer to fit the marked files.")
  153.  
  154. (defvar dired-flagging-regexp nil);; Last regexp used to flag files.
  155.  
  156. (defvar dired-file-version-alist)
  157.  
  158. (defvar dired-directory nil
  159.   "The directory name or shell wildcard that was used as argument to `ls'.
  160. Local to each dired buffer.  May be a list, in which case the car is the
  161. directory name and the cdr is the actual files to list.")
  162.  
  163. (defvar dired-actual-switches nil
  164.   "The value of `dired-listing-switches' used to make this buffer's text.")
  165.  
  166. (defvar dired-re-inode-size "[0-9 \t]*"
  167.   "Regexp for optional initial inode and file size as made by `ls -i -s'.")
  168.  
  169. ;; These regexps must be tested at beginning-of-line, but are also
  170. ;; used to search for next matches, so neither omitting "^" nor
  171. ;; replacing "^" by "\n" (to make it slightly faster) will work.
  172.  
  173. (defvar dired-re-mark "^[^ \n]")
  174. ;; "Regexp matching a marked line.
  175. ;; Important: the match ends just after the marker."
  176. (defvar dired-re-maybe-mark "^. ")
  177. (defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d"))
  178. (defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l"))
  179. (defvar dired-re-exe;; match ls permission string of an executable file
  180.   (mapconcat (function
  181.           (lambda (x)
  182.         (concat dired-re-maybe-mark dired-re-inode-size x)))
  183.          '("-[-r][-w][xs][-r][-w].[-r][-w]."
  184.            "-[-r][-w].[-r][-w][xs][-r][-w]."
  185.            "-[-r][-w].[-r][-w].[-r][-w][xst]")
  186.          "\\|"))
  187. (defvar dired-re-dot "^.* \\.\\.?$")
  188.  
  189. (defvar dired-subdir-alist nil
  190.   "Association list of subdirectories and their buffer positions.
  191. Each subdirectory has an element: (DIRNAME . STARTMARKER).
  192. The order of elements is the reverse of the order in the buffer.
  193. In simple cases, this list contains one element.")
  194.  
  195. (defvar dired-subdir-regexp "^. \\([^ \n\r]+\\)\\(:\\)[\n\r]"
  196.   "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
  197. Subexpression 1 is the subdirectory proper, no trailing colon.
  198. The match starts at the beginning of the line and ends after the end
  199. of the line (\\n or \\r).
  200. Subexpression 2 must end right before the \\n or \\r.")
  201.  
  202.  
  203. ;;; Macros must be defined before they are used, for the byte compiler.
  204.  
  205. ;; Mark all files for which CONDITION evals to non-nil.
  206. ;; CONDITION is evaluated on each line, with point at beginning of line.
  207. ;; MSG is a noun phrase for the type of files being marked.
  208. ;; It should end with a noun that can be pluralized by adding `s'.
  209. ;; Return value is the number of files marked, or nil if none were marked.
  210. (defmacro dired-mark-if (predicate msg)
  211.   (` (let (buffer-read-only count)
  212.        (save-excursion
  213.      (setq count 0)
  214.      (if (, msg) (message "Marking %ss..." (, msg)))
  215.      (goto-char (point-min))
  216.      (while (not (eobp))
  217.        (if (, predicate)
  218.            (progn
  219.          (delete-char 1)
  220.          (insert dired-marker-char)
  221.          (setq count (1+ count))))
  222.        (forward-line 1))
  223.      (if (, msg) (message "%s %s%s %s%s."
  224.               count
  225.               (, msg)
  226.               (dired-plural-s count)
  227.               (if (eq dired-marker-char ?\040) "un" "")
  228.               (if (eq dired-marker-char dired-del-marker)
  229.                   "flagged" "marked"))))
  230.        (and (> count 0) count))))
  231.  
  232. (defmacro dired-map-over-marks (body arg &optional show-progress)
  233. ;;  "Macro: Perform BODY with point somewhere on each marked line
  234. ;;and return a list of BODY's results.
  235. ;;If no marked file could be found, execute BODY on the current line.
  236. ;;  If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
  237. ;;  files instead of the marked files.
  238. ;;  In that case point is dragged along.  This is so that commands on
  239. ;;  the next ARG (instead of the marked) files can be chained easily.
  240. ;;  If ARG is otherwise non-nil, use current file instead.
  241. ;;If optional third arg SHOW-PROGRESS evaluates to non-nil,
  242. ;;   redisplay the dired buffer after each file is processed.
  243. ;;No guarantee is made about the position on the marked line.
  244. ;;  BODY must ensure this itself if it depends on this.
  245. ;;Search starts at the beginning of the buffer, thus the car of the list
  246. ;;  corresponds to the line nearest to the buffer's bottom.  This
  247. ;;  is also true for (positive and negative) integer values of ARG.
  248. ;;BODY should not be too long as it is expanded four times."
  249. ;;
  250. ;;Warning: BODY must not add new lines before point - this may cause an
  251. ;;endless loop.
  252. ;;This warning should not apply any longer, sk  2-Sep-1991 14:10.
  253.   (` (prog1
  254.      (let (buffer-read-only case-fold-search found results)
  255.        (if (, arg)
  256.            (if (integerp (, arg))
  257.            (progn;; no save-excursion, want to move point.
  258.              (dired-repeat-over-lines
  259.               (, arg)
  260.               (function (lambda ()
  261.                   (if (, show-progress) (sit-for 0))
  262.                   (setq results (cons (, body) results)))))
  263.              (if (< (, arg) 0)
  264.              (nreverse results)
  265.                results))
  266.          ;; non-nil, non-integer ARG means use current file:
  267.          (list (, body)))
  268.          (let ((regexp (dired-marker-regexp)) next-position)
  269.            (save-excursion
  270.          (goto-char (point-min))
  271.          ;; remember position of next marked file before BODY
  272.          ;; can insert lines before the just found file,
  273.          ;; confusing us by finding the same marked file again
  274.          ;; and again and...
  275.          (setq next-position (and (re-search-forward regexp nil t)
  276.                       (point-marker))
  277.                found (not (null next-position)))
  278.          (while next-position
  279.            (goto-char next-position)
  280.            (if (, show-progress) (sit-for 0))
  281.            (setq results (cons (, body) results))
  282.            ;; move after last match
  283.            (goto-char next-position)
  284.            (forward-line 1)
  285.            (set-marker next-position nil)
  286.            (setq next-position (and (re-search-forward regexp nil t)
  287.                         (point-marker)))))
  288.            (if found
  289.            results
  290.          (list (, body))))))
  291.        ;; save-excursion loses, again
  292.        (dired-move-to-filename))))
  293.  
  294. (defun dired-get-marked-files (&optional localp arg)
  295.   "Return the marked files' names as list of strings.
  296. The list is in the same order as the buffer, that is, the car is the
  297.   first marked file.
  298. Values returned are normally absolute pathnames.
  299. Optional arg LOCALP as in `dired-get-filename'.
  300. Optional second argument ARG forces to use other files.  If ARG is an
  301.   integer, use the next ARG files.  If ARG is otherwise non-nil, use
  302.   current file.  Usually ARG comes from the current prefix arg."
  303.   (save-excursion
  304.     (nreverse (dired-map-over-marks (dired-get-filename localp) arg))))
  305.  
  306.  
  307. ;; Function dired-ls is redefinable for VMS, ange-ftp, Prospero or
  308. ;; other special applications.
  309.  
  310. ;; The dired command
  311.  
  312. (defun dired-read-dir-and-switches (str)
  313.   ;; For use in interactive.
  314.   (reverse (list
  315.         (if current-prefix-arg
  316.         (read-string "Dired listing switches: "
  317.                  dired-listing-switches))
  318.         (read-file-name (format "Dired %s(directory): " str)
  319.                 nil default-directory nil))))
  320.  
  321. ;;;###autoload (define-key ctl-x-map "d" 'dired)
  322. ;;;###autoload
  323. (defun dired (dirname &optional switches)
  324.   "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
  325. Optional second argument SWITCHES specifies the `ls' options used.
  326. \(Interactively, use a prefix argument to be able to specify SWITCHES.)
  327. Dired displays a list of files in DIRNAME (which may also have
  328. shell wildcards appended to select certain files).  If DIRNAME is a cons,
  329. its first element is taken as the directory name and the resr as an explicit
  330. list of files to make directory entries for.
  331. \\<dired-mode-map>\
  332. You can move around in it with the usual commands.
  333. You can flag files for deletion with \\[dired-flag-file-deletion] and then
  334. delete them by typing \\[dired-do-flagged-delete].
  335. Type \\[describe-mode] after entering dired for more info.
  336.  
  337. If DIRNAME is already in a dired buffer, that buffer is used without refresh."
  338.   ;; Cannot use (interactive "D") because of wildcards.
  339.   (interactive (dired-read-dir-and-switches ""))
  340.   (switch-to-buffer (dired-noselect dirname switches)))
  341.  
  342. ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
  343. ;;;###autoload
  344. (defun dired-other-window (dirname &optional switches)
  345.   "\"Edit\" directory DIRNAME.  Like `dired' but selects in another window."
  346.   (interactive (dired-read-dir-and-switches "in other window "))
  347.   (switch-to-buffer-other-window (dired-noselect dirname switches)))
  348.  
  349. ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
  350. ;;;###autoload
  351. (defun dired-other-frame (dirname &optional switches)
  352.   "\"Edit\" directory DIRNAME.  Like `dired' but makes a new frame."
  353.   (interactive (dired-read-dir-and-switches "in other frame "))
  354.   (switch-to-buffer-other-frame (dired-noselect dirname switches)))
  355.  
  356. ;;;###autoload
  357. (defun dired-noselect (dir-or-list &optional switches)
  358.   "Like `dired' but returns the dired buffer as value, does not select it."
  359.   (or dir-or-list (setq dir-or-list default-directory))
  360.   ;; This loses the distinction between "/foo/*/" and "/foo/*" that
  361.   ;; some shells make:
  362.   (let (dirname)
  363.     (if (consp dir-or-list)
  364.     (setq dirname (car dir-or-list))
  365.       (setq dirname dir-or-list))
  366.     (setq dirname (expand-file-name (directory-file-name dirname)))
  367.     (if (file-directory-p dirname)
  368.     (setq dirname (file-name-as-directory dirname)))
  369.     (if (consp dir-or-list)
  370.     (setq dir-or-list (cons dirname (cdr dir-or-list)))
  371.       (setq dir-or-list dirname))
  372.     (dired-internal-noselect dir-or-list switches)))
  373.  
  374. ;; Separate function from dired-noselect for the sake of dired-vms.el.
  375. (defun dired-internal-noselect (dir-or-list &optional switches)
  376.   ;; If there is an existing dired buffer for DIRNAME, just leave
  377.   ;; buffer as it is (don't even call dired-revert).
  378.   ;; This saves time especially for deep trees or with ange-ftp.
  379.   ;; The user can type `g'easily, and it is more consistent with find-file.
  380.   ;; But if SWITCHES are given they are probably different from the
  381.   ;; buffer's old value, so call dired-sort-other, which does
  382.   ;; revert the buffer.
  383.   ;; A pity we can't possibly do "Directory has changed - refresh? "
  384.   ;; like find-file does.
  385.   (let* ((dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
  386.      (buffer (dired-find-buffer-nocreate dir-or-list))
  387.      ;; note that buffer already is in dired-mode, if found
  388.      (new-buffer-p (not buffer))
  389.      (old-buf (current-buffer)))
  390.     (or buffer
  391.     (let ((default-major-mode 'fundamental-mode))
  392.       ;; We don't want default-major-mode to run hooks and set auto-fill
  393.       ;; or whatever, now that dired-mode does not
  394.       ;; kill-all-local-variables any longer.
  395.       (setq buffer (create-file-buffer (directory-file-name dirname)))))
  396.     (set-buffer buffer)
  397.     (if (not new-buffer-p)        ; existing buffer ...
  398.     (if switches            ; ... but new switches
  399.         (dired-sort-other switches))    ; this calls dired-revert
  400.       ;; Else a new buffer
  401.       (setq default-directory
  402.         (abbreviate-file-name
  403.          (if (file-directory-p dirname)
  404.          dirname
  405.            (file-name-directory dirname))))
  406.       (or switches (setq switches dired-listing-switches))
  407.       (dired-mode dirname switches)
  408.       ;; default-directory and dired-actual-switches are set now
  409.       ;; (buffer-local), so we can call dired-readin:
  410.       (let ((failed t))
  411.     (unwind-protect
  412.         (progn (dired-readin dir-or-list buffer)
  413.            (setq failed nil))
  414.       ;; dired-readin can fail if parent directories are inaccessible.
  415.       ;; Don't leave an empty buffer around in that case.
  416.       (if failed (kill-buffer buffer))))
  417.       ;; No need to narrow since the whole buffer contains just
  418.       ;; dired-readin's output, nothing else.  The hook can
  419.       ;; successfully use dired functions (e.g. dired-get-filename)
  420.       ;; as the subdir-alist has been built in dired-readin.
  421.       (run-hooks 'dired-after-readin-hook)
  422.       (goto-char (point-min))
  423.       (dired-initial-position dirname))
  424.     (set-buffer old-buf)
  425.     buffer))
  426.  
  427. ;; This differs from dired-buffers-for-dir in that it does not consider
  428. ;; subdirs of default-directory and searches for the first match only
  429. (defun dired-find-buffer-nocreate (dirname)
  430.   (let (found (blist (buffer-list)))
  431.     (while blist
  432.       (save-excursion
  433.         (set-buffer (car blist))
  434.     (if (and (eq major-mode 'dired-mode)
  435.          (equal dired-directory dirname))
  436.         (setq found (car blist)
  437.           blist nil)
  438.       (setq blist (cdr blist)))))
  439.     found))
  440.  
  441.  
  442. ;; Read in a new dired buffer
  443.  
  444. ;; dired-readin differs from dired-insert-subdir in that it accepts
  445. ;; wildcards, erases the buffer, and builds the subdir-alist anew
  446. ;; (including making it buffer-local and clearing it first).
  447. (defun dired-readin (dir-or-list buffer)
  448.   ;; default-directory and dired-actual-switches must be buffer-local
  449.   ;; and initialized by now.
  450.   ;; Thus we can test (equal default-directory dirname) instead of
  451.   ;; (file-directory-p dirname) and save a filesystem transaction.
  452.   ;; Also, we can run this hook which may want to modify the switches
  453.   ;; based on default-directory, e.g. with ange-ftp to a SysV host
  454.   ;; where ls won't understand -Al switches.
  455.   (let (dirname)
  456.     (if (consp dir-or-list)
  457.     (setq dirname (car dir-or-list))
  458.       (setq dirname dir-or-list))
  459.     (setq dirname (expand-file-name dirname))
  460.     (if (consp dir-or-list)
  461.     (setq dir-or-list (cons dirname (cdr dir-or-list))))
  462.     (run-hooks 'dired-before-readin-hook)
  463.     (save-excursion
  464.       (message "Reading directory %s..." dirname)
  465.       (set-buffer buffer)
  466.       (let (buffer-read-only (failed t))
  467.     (widen)
  468.     (erase-buffer)
  469.     (dired-readin-insert dir-or-list)
  470.     (indent-rigidly (point-min) (point-max) 2)
  471.     ;; We need this to make the root dir have a header line as all
  472.     ;; other subdirs have:
  473.     (goto-char (point-min))
  474.     (dired-insert-headerline default-directory)
  475.     ;; can't run dired-after-readin-hook here, it may depend on the subdir
  476.     ;; alist to be OK.
  477.     )
  478.       (message "Reading directory %s...done" dirname)
  479.       (set-buffer-modified-p nil)
  480.       ;; Must first make alist buffer local and set it to nil because
  481.       ;; dired-build-subdir-alist will call dired-clear-alist first
  482.       (set (make-local-variable 'dired-subdir-alist) nil)
  483.       (dired-build-subdir-alist))))
  484.  
  485. ;; Subroutines of dired-readin
  486.  
  487. (defun dired-readin-insert (dir-or-list)
  488.   ;; Just insert listing for the passed-in directory or
  489.   ;; directory-and-file list, assuming a clean buffer.
  490.   (let (dirname)
  491.     (if (consp dir-or-list)
  492.     (setq dirname (car dir-or-list))
  493.       (setq dirname dir-or-list))
  494.     ;; Expand before comparing in case one or both have been abbreviated.
  495.     (if (and (equal (expand-file-name default-directory)
  496.             (expand-file-name dirname))
  497.          (not (consp dir-or-list)))
  498.     ;; If we are reading a whole single directory...
  499.     (dired-insert-directory dir-or-list dired-actual-switches nil t)
  500.       (if (not (file-readable-p
  501.         (directory-file-name (file-name-directory dirname))))
  502.       (error "Directory %s inaccessible or nonexistent" dirname)
  503.     ;; Else assume it contains wildcards,
  504.     ;; unless it is an explicit list of files.
  505.     (dired-insert-directory dir-or-list dired-actual-switches
  506.                 (not (listp dir-or-list)))
  507.     (save-excursion        ;; insert wildcard instead of total line:
  508.       (goto-char (point-min))
  509.       (insert "wildcard " (file-name-nondirectory dirname) "\n"))))))
  510.  
  511. (defun dired-insert-directory (dir-or-list switches &optional wildcard full-p)
  512.   ;; Do the right thing whether dir-or-list is atomic or not.  If it is,
  513.   ;; inset all files listed in the cdr (the car is the passed-in directory
  514.   ;; list.
  515.   (if (consp dir-or-list)
  516.       (progn
  517.     (mapcar
  518.      (function (lambda (x) (insert-directory x switches wildcard full-p)))
  519.      (cdr dir-or-list)))
  520.     (insert-directory dir-or-list switches wildcard full-p))
  521.   (setq dired-directory dir-or-list))
  522.  
  523. (defun dired-insert-headerline (dir);; also used by dired-insert-subdir
  524.   ;; Insert DIR's headerline with no trailing slash, exactly like ls
  525.   ;; would, and put cursor where dired-build-subdir-alist puts subdir
  526.   ;; boundaries.
  527.   (save-excursion (insert "  " (directory-file-name dir) ":\n")))
  528.  
  529.  
  530. ;; Reverting a dired buffer
  531.  
  532. (defun dired-revert (&optional arg noconfirm)
  533.   ;; Reread the dired buffer.  Must also be called after
  534.   ;; dired-actual-switches have changed.
  535.   ;; Should not fail even on completely garbaged buffers.
  536.   ;; Preserves old cursor, marks/flags, hidden-p.
  537.   (widen)                ; just in case user narrowed
  538.   (let ((opoint (point))
  539.     (ofile (dired-get-filename nil t))
  540.     (mark-alist nil)        ; save marked files
  541.     (hidden-subdirs (dired-remember-hidden))
  542.     (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
  543.     (case-fold-search nil)        ; we check for upper case ls flags
  544.     buffer-read-only)
  545.     (goto-char (point-min))
  546.     (setq mark-alist;; only after dired-remember-hidden since this unhides:
  547.       (dired-remember-marks (point-min) (point-max)))
  548.     ;; treat top level dir extra (it may contain wildcards)
  549.     (dired-uncache
  550.      (if (consp dired-directory) (car dired-directory) dired-directory))
  551.     (dired-readin dired-directory (current-buffer))
  552.     (let ((dired-after-readin-hook nil))
  553.       ;; don't run that hook for each subdir...
  554.       (dired-insert-old-subdirs old-subdir-alist))
  555.     (dired-mark-remembered mark-alist)    ; mark files that were marked
  556.     ;; ... run the hook for the whole buffer, and only after markers
  557.     ;; have been reinserted (else omitting in dired-x would omit marked files)
  558.     (run-hooks 'dired-after-readin-hook)    ; no need to narrow
  559.     (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
  560.     (goto-char opoint))        ; was before
  561.     (dired-move-to-filename)
  562.     (save-excursion            ; hide subdirs that were hidden
  563.       (mapcar (function (lambda (dir)
  564.               (if (dired-goto-subdir dir)
  565.                   (dired-hide-subdir 1))))
  566.           hidden-subdirs)))
  567.   ;; outside of the let scope
  568. ;;; Might as well not override the user if the user changed this.
  569. ;;;  (setq buffer-read-only t)
  570.   )
  571.  
  572. ;; Subroutines of dired-revert
  573. ;; Some of these are also used when inserting subdirs.
  574.  
  575. (defun dired-remember-marks (beg end)
  576.   ;; Return alist of files and their marks, from BEG to END.
  577.   (if selective-display            ; must unhide to make this work.
  578.       (let (buffer-read-only)
  579.     (subst-char-in-region beg end ?\r ?\n)))
  580.   (let (fil chr alist)
  581.     (save-excursion
  582.       (goto-char beg)
  583.       (while (re-search-forward dired-re-mark end t)
  584.     (if (setq fil (dired-get-filename nil t))
  585.         (setq chr (preceding-char)
  586.           alist (cons (cons fil chr) alist)))))
  587.     alist))
  588.  
  589. ;; Mark all files remembered in ALIST.
  590. ;; Each element of ALIST looks like (FILE . MARKERCHAR).
  591. (defun dired-mark-remembered (alist)
  592.   (let (elt fil chr)
  593.     (while alist
  594.       (setq elt (car alist)
  595.         alist (cdr alist)
  596.         fil (car elt)
  597.         chr (cdr elt))
  598.       (if (dired-goto-file fil)
  599.       (save-excursion
  600.         (beginning-of-line)
  601.         (delete-char 1)
  602.         (insert chr))))))
  603.  
  604. ;; Return a list of names of subdirs currently hidden.
  605. (defun dired-remember-hidden ()
  606.   (let ((l dired-subdir-alist) dir pos result)
  607.     (while l
  608.       (setq dir (car (car l))
  609.         pos (cdr (car l))
  610.         l (cdr l))
  611.       (goto-char pos)
  612.       (skip-chars-forward "^\r\n")
  613.       (if (eq (following-char) ?\r)
  614.       (setq result (cons dir result))))
  615.     result))
  616.  
  617. ;; Try to insert all subdirs that were displayed before,
  618. ;; according to the former subdir alist OLD-SUBDIR-ALIST.
  619. (defun dired-insert-old-subdirs (old-subdir-alist)
  620.   (or (string-match "R" dired-actual-switches)
  621.       (let (elt dir)
  622.     (while old-subdir-alist
  623.       (setq elt (car old-subdir-alist)
  624.         old-subdir-alist (cdr old-subdir-alist)
  625.         dir (car elt))
  626.       (condition-case ()
  627.           (progn
  628.         (dired-uncache dir)
  629.         (dired-insert-subdir dir))
  630.         (error nil))))))
  631.  
  632. ;; Remove directory DIR from any directory cache.
  633. (defun dired-uncache (dir)
  634.   (let ((handler (find-file-name-handler dir)))
  635.     (if handler
  636.     (funcall handler 'dired-uncache dir))))
  637.  
  638. ;; dired mode key bindings and initialization
  639.  
  640. (defvar dired-mode-map nil "Local keymap for dired-mode buffers.")
  641. (if dired-mode-map
  642.     nil
  643.   ;; Force `f' rather than `e' in the mode doc:
  644.   (defalias 'dired-advertised-find-file 'dired-find-file)
  645.   ;; This looks ugly when substitute-command-keys uses C-d instead d:
  646.   ;;  (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
  647.  
  648.   (setq dired-mode-map (make-keymap))
  649.   (suppress-keymap dired-mode-map)
  650.   ;; Commands to mark or flag certain categories of files
  651.   (define-key dired-mode-map "#" 'dired-flag-auto-save-files)
  652.   (define-key dired-mode-map "*" 'dired-mark-executables)
  653.   (define-key dired-mode-map "." 'dired-clean-directory)
  654.   (define-key dired-mode-map "/" 'dired-mark-directories)
  655.   (define-key dired-mode-map "@" 'dired-mark-symlinks)
  656.   (define-key dired-mode-map "~" 'dired-flag-backup-files)
  657.   ;; Upper case keys (except !) for operating on the marked files
  658.   (define-key dired-mode-map "C" 'dired-do-copy)
  659.   (define-key dired-mode-map "B" 'dired-do-byte-compile)
  660.   (define-key dired-mode-map "D" 'dired-do-delete)
  661.   (define-key dired-mode-map "G" 'dired-do-chgrp)
  662.   (define-key dired-mode-map "H" 'dired-do-hardlink)
  663.   (define-key dired-mode-map "L" 'dired-do-load)
  664.   (define-key dired-mode-map "M" 'dired-do-chmod)
  665.   (define-key dired-mode-map "O" 'dired-do-chown)
  666.   (define-key dired-mode-map "P" 'dired-do-print)
  667.   (define-key dired-mode-map "R" 'dired-do-rename)
  668.   (define-key dired-mode-map "S" 'dired-do-symlink)
  669.   (define-key dired-mode-map "X" 'dired-do-shell-command)
  670.   (define-key dired-mode-map "Z" 'dired-do-compress)
  671.   (define-key dired-mode-map "!" 'dired-do-shell-command)
  672.   ;; Comparison commands
  673.   (define-key dired-mode-map "=" 'dired-diff)
  674.   (define-key dired-mode-map "\M-=" 'dired-backup-diff)
  675.   ;; Tree Dired commands
  676.   (define-key dired-mode-map "\M-\C-?" 'dired-unmark-all-files)
  677.   (define-key dired-mode-map "\M-\C-d" 'dired-tree-down)
  678.   (define-key dired-mode-map "\M-\C-u" 'dired-tree-up)
  679.   (define-key dired-mode-map "\M-\C-n" 'dired-next-subdir)
  680.   (define-key dired-mode-map "\M-\C-p" 'dired-prev-subdir)
  681.   ;; move to marked files
  682.   (define-key dired-mode-map "\M-{" 'dired-prev-marked-file)
  683.   (define-key dired-mode-map "\M-}" 'dired-next-marked-file)
  684.   ;; Make all regexp commands share a `%' prefix:
  685.   (defalias 'dired-regexp-prefix (make-sparse-keymap))
  686.   (define-key dired-mode-map "%" 'dired-regexp-prefix)
  687.   (define-key dired-mode-map "%u" 'dired-upcase)
  688.   (define-key dired-mode-map "%l" 'dired-downcase)
  689.   (define-key dired-mode-map "%d" 'dired-flag-files-regexp)
  690.   (define-key dired-mode-map "%m" 'dired-mark-files-regexp)
  691.   (define-key dired-mode-map "%r" 'dired-do-rename-regexp)
  692.   (define-key dired-mode-map "%C" 'dired-do-copy-regexp)
  693.   (define-key dired-mode-map "%H" 'dired-do-hardlink-regexp)
  694.   (define-key dired-mode-map "%R" 'dired-do-rename-regexp)
  695.   (define-key dired-mode-map "%S" 'dired-do-symlink-regexp)
  696.   ;; Lower keys for commands not operating on all the marked files
  697.   (define-key dired-mode-map "c" 'dired-change-marks)
  698.   (define-key dired-mode-map "d" 'dired-flag-file-deletion)
  699.   (define-key dired-mode-map "e" 'dired-find-file)
  700.   (define-key dired-mode-map "f" 'dired-advertised-find-file)
  701.   (define-key dired-mode-map "g" 'revert-buffer)
  702.   (define-key dired-mode-map "h" 'describe-mode)
  703.   (define-key dired-mode-map "i" 'dired-maybe-insert-subdir)
  704.   (define-key dired-mode-map "k" 'dired-do-kill-lines)
  705.   (define-key dired-mode-map "l" 'dired-do-redisplay)
  706.   (define-key dired-mode-map "m" 'dired-mark)
  707.   (define-key dired-mode-map "n" 'dired-next-line)
  708.   (define-key dired-mode-map "o" 'dired-find-file-other-window)
  709.   (define-key dired-mode-map "\C-o" 'dired-display-file)
  710.   (define-key dired-mode-map "p" 'dired-previous-line)
  711.   (define-key dired-mode-map "q" 'dired-quit)
  712.   (define-key dired-mode-map "s" 'dired-sort-toggle-or-edit)
  713.   (define-key dired-mode-map "u" 'dired-unmark)
  714.   (define-key dired-mode-map "v" 'dired-view-file)
  715.   (define-key dired-mode-map "x" 'dired-do-flagged-delete)
  716.   (define-key dired-mode-map "+" 'dired-create-directory)
  717.   ;; moving
  718.   (define-key dired-mode-map "<" 'dired-prev-dirline)
  719.   (define-key dired-mode-map ">" 'dired-next-dirline)
  720.   (define-key dired-mode-map "^" 'dired-up-directory)
  721.   (define-key dired-mode-map " "  'dired-next-line)
  722.   (define-key dired-mode-map "\C-n" 'dired-next-line)
  723.   (define-key dired-mode-map "\C-p" 'dired-previous-line)
  724.   ;; hiding
  725.   (define-key dired-mode-map "$" 'dired-hide-subdir)
  726.   (define-key dired-mode-map "\M-$" 'dired-hide-all)
  727.   ;; misc
  728.   (define-key dired-mode-map "?" 'dired-summary)
  729.   (define-key dired-mode-map "\177" 'dired-unmark-backward)
  730.   (define-key dired-mode-map "\C-_" 'dired-undo)
  731.   (define-key dired-mode-map "\C-xu" 'dired-undo)
  732.   )
  733.  
  734. ;; Make menu bar items.
  735.  
  736. ;; Get rid of the Edit menu bar item to save space.
  737. (define-key dired-mode-map [menu-bar edit] 'undefined)
  738.  
  739. (define-key dired-mode-map [menu-bar subdir]
  740.   (cons "Subdir" (make-sparse-keymap "Subdir")))
  741.  
  742. (define-key dired-mode-map [menu-bar subdir hide-all]
  743.   '("Hide All" . dired-hide-all))
  744. (define-key dired-mode-map [menu-bar subdir hide-subdir]
  745.   '("Hide Subdir" . dired-hide-subdir))
  746. (define-key dired-mode-map [menu-bar subdir tree-down]
  747.   '("Tree Down" . dired-tree-down))
  748. (define-key dired-mode-map [menu-bar subdir tree-up]
  749.   '("Tree Up" . dired-tree-up))
  750. (define-key dired-mode-map [menu-bar subdir up]
  751.   '("Up Directory" . dired-up-directory))
  752. (define-key dired-mode-map [menu-bar subdir prev-subdir]
  753.   '("Prev Subdir" . dired-prev-subdir))
  754. (define-key dired-mode-map [menu-bar subdir next-subdir]
  755.   '("Next Subdir" . dired-next-subdir))
  756. (define-key dired-mode-map [menu-bar subdir prev-dirline]
  757.   '("Prev Dirline" . dired-prev-dirline))
  758. (define-key dired-mode-map [menu-bar subdir next-dirline]
  759.   '("Next Dirline" . dired-next-dirline))
  760. (define-key dired-mode-map [menu-bar subdir insert]
  761.   '("Insert This Subdir" . dired-maybe-insert-subdir))
  762.  
  763. (define-key dired-mode-map [menu-bar immediate]
  764.   (cons "Immediate" (make-sparse-keymap "Immediate")))
  765.  
  766. (define-key dired-mode-map [menu-bar immediate backup-diff]
  767.   '("Compare with Backup" . dired-backup-diff))
  768. (define-key dired-mode-map [menu-bar immediate diff]
  769.   '("Diff" . dired-diff))
  770. (define-key dired-mode-map [menu-bar immediate view]
  771.   '("View This File" . dired-view-file))
  772. (define-key dired-mode-map [menu-bar immediate display]
  773.   '("Display in Other Window" . dired-display-file))
  774. (define-key dired-mode-map [menu-bar immediate find-file-other-window]
  775.   '("Find in Other Window" . dired-find-file-other-window))
  776. (define-key dired-mode-map [menu-bar immediate find-file]
  777.   '("Find This File" . dired-find-file))
  778. (define-key dired-mode-map [menu-bar immediate create-directory]
  779.   '("Create Directory..." . dired-create-directory))
  780.  
  781. (define-key dired-mode-map [menu-bar regexp]
  782.   (cons "Regexp" (make-sparse-keymap "Regexp")))
  783.  
  784. (define-key dired-mode-map [menu-bar regexp downcase]
  785.   '("Downcase" . dired-downcase))
  786. (define-key dired-mode-map [menu-bar regexp upcase]
  787.   '("Upcase" . dired-upcase))
  788. (define-key dired-mode-map [menu-bar regexp hardlink]
  789.   '("Hardlink..." . dired-do-hardlink-regexp))
  790. (define-key dired-mode-map [menu-bar regexp symlink]
  791.   '("Symlink..." . dired-do-symlink-regexp))
  792. (define-key dired-mode-map [menu-bar regexp rename]
  793.   '("Rename..." . dired-do-rename-regexp))
  794. (define-key dired-mode-map [menu-bar regexp copy]
  795.   '("Copy..." . dired-do-copy-regexp))
  796. (define-key dired-mode-map [menu-bar regexp flag]
  797.   '("Flag..." . dired-flag-files-regexp))
  798. (define-key dired-mode-map [menu-bar regexp mark]
  799.   '("Mark..." . dired-mark-files-regexp))
  800.  
  801. (define-key dired-mode-map [menu-bar mark]
  802.   (cons "Mark" (make-sparse-keymap "Mark")))
  803.  
  804. (define-key dired-mode-map [menu-bar mark prev]
  805.   '("Previous Marked" . dired-prev-marked-file))
  806. (define-key dired-mode-map [menu-bar mark next]
  807.   '("Next Marked" . dired-next-marked-file))
  808. (define-key dired-mode-map [menu-bar mark marks]
  809.   '("Change Marks..." . dired-change-marks))
  810. (define-key dired-mode-map [menu-bar mark unmark-all]
  811.   '("Unmark All" . dired-unmark-all-files-no-query))
  812. (define-key dired-mode-map [menu-bar mark symlinks]
  813.   '("Mark Symlinks" . dired-mark-symlinks))
  814. (define-key dired-mode-map [menu-bar mark directories]
  815.   '("Mark Directories" . dired-mark-directories))
  816. (define-key dired-mode-map [menu-bar mark directory]
  817.   '("Mark Old Backups" . dired-clean-directory))
  818. (define-key dired-mode-map [menu-bar mark executables]
  819.   '("Mark Executables" . dired-mark-executables))
  820. (define-key dired-mode-map [menu-bar mark backup-files]
  821.   '("Flag Backup Files" . dired-flag-backup-files))
  822. (define-key dired-mode-map [menu-bar mark auto-save-files]
  823.   '("Flag Auto-save Files" . dired-flag-auto-save-files))
  824. (define-key dired-mode-map [menu-bar mark deletion]
  825.   '("Flag" . dired-flag-file-deletion))
  826. (define-key dired-mode-map [menu-bar mark unmark]
  827.   '("Unmark" . dired-unmark))
  828. (define-key dired-mode-map [menu-bar mark mark]
  829.   '("Mark" . dired-mark))
  830.  
  831. (define-key dired-mode-map [menu-bar operate]
  832.   (cons "Operate" (make-sparse-keymap "Operate")))
  833.  
  834. (define-key dired-mode-map [menu-bar operate chown]
  835.   '("Change Owner..." . dired-do-chown))
  836. (define-key dired-mode-map [menu-bar operate chgrp]
  837.   '("Change Group..." . dired-do-chgrp))
  838. (define-key dired-mode-map [menu-bar operate chmod]
  839.   '("Change Mode..." . dired-do-chmod))
  840. (define-key dired-mode-map [menu-bar operate load]
  841.   '("Load" . dired-do-load))
  842. (define-key dired-mode-map [menu-bar operate compile]
  843.   '("Byte-compile" . dired-do-byte-compile))
  844. (define-key dired-mode-map [menu-bar operate compress]
  845.   '("Compress" . dired-do-compress))
  846. (define-key dired-mode-map [menu-bar operate print]
  847.   '("Print" . dired-do-print))
  848. (define-key dired-mode-map [menu-bar operate hardlink]
  849.   '("Hardlink to..." . dired-do-hardlink))
  850. (define-key dired-mode-map [menu-bar operate symlink]
  851.   '("Symlink to..." . dired-do-symlink))
  852. (define-key dired-mode-map [menu-bar operate command]
  853.   '("Shell Command..." . dired-do-shell-command))
  854. (define-key dired-mode-map [menu-bar operate delete]
  855.   '("Delete" . dired-do-delete))
  856. (define-key dired-mode-map [menu-bar operate rename]
  857.   '("Rename to..." . dired-do-rename))
  858. (define-key dired-mode-map [menu-bar operate copy]
  859.   '("Copy to..." . dired-do-copy))
  860.  
  861. ;; Dired mode is suitable only for specially formatted data.
  862. (put 'dired-mode 'mode-class 'special)
  863.  
  864. (defun dired-mode (&optional dirname switches)
  865.   "\
  866. Mode for \"editing\" directory listings.
  867. In dired, you are \"editing\" a list of the files in a directory and
  868.   \(optionally) its subdirectories, in the format of `ls -lR'.
  869.   Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
  870. \"Editing\" means that you can run shell commands on files, visit,
  871.   compress, load or byte-compile them, change their file attributes
  872.   and insert subdirectories into the same buffer.  You can \"mark\"
  873.   files for later commands or \"flag\" them for deletion, either file
  874.   by file or all files matching certain criteria.
  875. You can move using the usual cursor motion commands.\\<dired-mode-map>
  876. Letters no longer insert themselves.  Digits are prefix arguments.
  877. Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
  878. Type \\[dired-mark] to Mark a file or subdirectory for later commands.
  879.   Most commands operate on the marked files and use the current file
  880.   if no files are marked.  Use a numeric prefix argument to operate on
  881.   the next ARG (or previous -ARG if ARG<0) files, or just `1'
  882.   to operate on the current file only.  Prefix arguments override marks.
  883.   Mark-using commands display a list of failures afterwards.  Type \\[dired-summary]
  884.   to see why something went wrong.
  885. Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
  886. Type \\[dired-unmark-backward] to back up one line and unflag.
  887. Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
  888. Type \\[dired-advertised-find-file] to Find the current line's file
  889.   (or dired it in another buffer, if it is a directory).
  890. Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
  891. Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
  892. Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
  893. Type \\[dired-do-copy] to Copy files.
  894. Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
  895. Type \\[revert-buffer] to read all currently expanded directories again.
  896.   This retains all marks and hides subdirs again that were hidden before.
  897. SPC and DEL can be used to move down and up by lines.
  898.  
  899. If dired ever gets confused, you can either type \\[revert-buffer] \
  900. to read the
  901. directories again, type \\[dired-do-redisplay] \
  902. to relist a single or the marked files or a
  903. subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
  904. again for the directory tree.
  905.  
  906. Customization variables (rename this buffer and type \\[describe-variable] on each line
  907. for more info):
  908.  
  909.   dired-listing-switches
  910.   dired-trivial-filenames
  911.   dired-shrink-to-fit
  912.   dired-marker-char
  913.   dired-del-marker
  914.   dired-keep-marker-rename
  915.   dired-keep-marker-copy
  916.   dired-keep-marker-hardlink
  917.   dired-keep-marker-symlink
  918.  
  919. Hooks (use \\[describe-variable] to see their documentation):
  920.  
  921.   dired-before-readin-hook
  922.   dired-after-readin-hook
  923.   dired-mode-hook
  924.   dired-load-hook
  925.  
  926. Keybindings:
  927. \\{dired-mode-map}"
  928.   ;; Not to be called interactively (e.g. dired-directory will be set
  929.   ;; to default-directory, which is wrong with wildcards).
  930.   (kill-all-local-variables)
  931.   (use-local-map dired-mode-map)
  932.   (dired-advertise)            ; default-directory is already set
  933.   (setq major-mode 'dired-mode
  934.     mode-name "Dired"
  935. ;;    case-fold-search nil
  936.     buffer-read-only t
  937.     selective-display t        ; for subdirectory hiding
  938.     mode-line-buffer-identification '("Dired: %17b"))
  939.   (set (make-local-variable 'revert-buffer-function)
  940.        (function dired-revert))
  941.   (set (make-local-variable 'page-delimiter)
  942.        "\n\n")
  943.   (set (make-local-variable 'dired-directory)
  944.        (or dirname default-directory))
  945.   ;; list-buffers uses this to display the dir being edited in this buffer.
  946.   (set (make-local-variable 'list-buffers-directory)
  947.        dired-directory)
  948.   (set (make-local-variable 'dired-actual-switches)
  949.        (or switches dired-listing-switches))
  950.   (dired-sort-other dired-actual-switches t)
  951.   (run-hooks 'dired-mode-hook))
  952.  
  953. ;; Idiosyncratic dired commands that don't deal with marks.
  954.  
  955. (defun dired-quit ()
  956.   "Bury the current dired buffer."
  957.   (interactive)
  958.   (bury-buffer))
  959.  
  960. (defun dired-summary ()
  961.   "Summarize basic Dired commands and show recent Dired errors."
  962.   (interactive)
  963.   (dired-why)
  964.   ;>> this should check the key-bindings and use substitute-command-keys if non-standard
  965.   (message
  966.    "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
  967.  
  968. (defun dired-undo ()
  969.   "Undo in a dired buffer.
  970. This doesn't recover lost files, it just undoes changes in the buffer itself.
  971. You can use it to recover marks, killed lines or subdirs.
  972. In the latter case, you have to do \\[dired-build-subdir-alist] to
  973. parse the buffer again."
  974.   (interactive)
  975.   (let (buffer-read-only)
  976.     (undo)))
  977.  
  978. (defun dired-next-line (arg)
  979.   "Move down lines then position at filename.
  980. Optional prefix ARG says how many lines to move; default is one line."
  981.   (interactive "p")
  982.   (next-line arg)
  983.   (dired-move-to-filename))
  984.  
  985. (defun dired-previous-line (arg)
  986.   "Move up lines then position at filename.
  987. Optional prefix ARG says how many lines to move; default is one line."
  988.   (interactive "p")
  989.   (previous-line arg)
  990.   (dired-move-to-filename))
  991.  
  992. (defun dired-next-dirline (arg &optional opoint)
  993.   "Goto ARG'th next directory file line."
  994.   (interactive "p")
  995.   (or opoint (setq opoint (point)))
  996.   (if (if (> arg 0)
  997.       (re-search-forward dired-re-dir nil t arg)
  998.     (beginning-of-line)
  999.     (re-search-backward dired-re-dir nil t (- arg)))
  1000.       (dired-move-to-filename)        ; user may type `i' or `f'
  1001.     (goto-char opoint)
  1002.     (error "No more subdirectories")))
  1003.  
  1004. (defun dired-prev-dirline (arg)
  1005.   "Goto ARG'th previous directory file line."
  1006.   (interactive "p")
  1007.   (dired-next-dirline (- arg)))
  1008.  
  1009. (defun dired-up-directory ()
  1010.   "Run dired on parent directory of current directory.
  1011. Find the parent directory either in this buffer or another buffer.
  1012. Creates a buffer if necessary."
  1013.   (interactive)
  1014.   (let* ((dir (dired-current-directory))
  1015.      (up (file-name-directory (directory-file-name dir))))
  1016.     (or (dired-goto-file (directory-file-name dir))
  1017.     ;; Only try dired-goto-subdir if buffer has more than one dir.
  1018.     (and (cdr dired-subdir-alist)
  1019.          (dired-goto-subdir up))
  1020.     (progn
  1021.       (dired 
  1022. up)
  1023.       (dired-goto-file dir)))))
  1024.  
  1025. (defun dired-find-file ()
  1026.   "In dired, visit the file or directory named on this line."
  1027.   (interactive)
  1028.   (find-file (file-name-sans-versions (dired-get-filename) t)))
  1029.  
  1030. (defun dired-view-file ()
  1031.   "In dired, examine a file in view mode, returning to dired when done.
  1032. When file is a directory, show it in this buffer if it is inserted;
  1033. otherwise, display it in another buffer."
  1034.   (interactive)
  1035.   (if (file-directory-p (dired-get-filename))
  1036.       (or (and (cdr dired-subdir-alist)
  1037.            (dired-goto-subdir (dired-get-filename)))
  1038.       (dired (dired-get-filename)))
  1039.     (view-file (dired-get-filename))))
  1040.  
  1041. (defun dired-find-file-other-window ()
  1042.   "In dired, visit this file or directory in another window."
  1043.   (interactive)
  1044.   (find-file-other-window (file-name-sans-versions (dired-get-filename) t)))
  1045.  
  1046. (defun dired-display-file ()
  1047.   "In dired, display this file or directory in another window."
  1048.   (interactive)
  1049.   (let ((file (file-name-sans-versions (dired-get-filename) t)))
  1050.     (display-buffer (find-file-noselect file))))
  1051.  
  1052. ;;; Functions for extracting and manipulating file names in dired buffers.
  1053.  
  1054. (defun dired-get-filename (&optional localp no-error-if-not-filep)
  1055.   "In dired, return name of file mentioned on this line.
  1056. Value returned normally includes the directory name.
  1057. Optional arg LOCALP with value `no-dir' means don't include directory
  1058.   name in result.  A value of t means construct name relative to
  1059.   `default-directory', which still may contain slashes if in a subdirectory.
  1060. Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
  1061.   this line, otherwise an error occurs."
  1062.   (let (case-fold-search file p1 p2)
  1063.     (save-excursion
  1064.       (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
  1065.       (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
  1066.     ;; nil if no file on this line, but no-error-if-not-filep is t:
  1067.     (if (setq file (and p1 p2 (buffer-substring p1 p2)))
  1068.     ;; Check if ls quoted the names, and unquote them.
  1069.     ;; Using read to unquote is much faster than substituting
  1070.     ;; \007 (4 chars) -> ^G  (1 char) etc. in a lisp loop.
  1071.     (cond ((string-match "b" dired-actual-switches) ; System V ls
  1072.            ;; This case is about 20% slower than without -b.
  1073.            (setq file
  1074.              (read
  1075.               (concat "\""
  1076.                   ;; some ls -b don't escape quotes, argh!
  1077.                   ;; This is not needed for GNU ls, though.
  1078.                   (or (dired-string-replace-match
  1079.                    "\\([^\\]\\)\"" file "\\1\\\\\"")
  1080.                   file)
  1081.                   "\""))))
  1082.           ;; If you do this, update dired-insert-subdir-validate too
  1083.           ;; ((string-match "Q" dired-actual-switches) ; GNU ls
  1084.           ;;  (setq file (read file)))
  1085.           ))
  1086.     (if (eq localp 'no-dir)
  1087.     file
  1088.       (and file (concat (dired-current-directory localp) file)))))
  1089.  
  1090. (defun dired-make-absolute (file &optional dir)
  1091.   ;;"Convert FILE (a pathname relative to DIR) to an absolute pathname."
  1092.   ;; We can't always use expand-file-name as this would get rid of `.'
  1093.   ;; or expand in / instead default-directory if DIR=="".
  1094.   ;; This should be good enough for ange-ftp, but might easily be
  1095.   ;; redefined (for VMS?).
  1096.   ;; It should be reasonably fast, though, as it is called in
  1097.   ;; dired-get-filename.
  1098.   (concat (or dir default-directory) file))
  1099.  
  1100. (defun dired-make-relative (file &optional dir no-error)
  1101.   ;;"Convert FILE (an absolute pathname) to a pathname relative to DIR.
  1102.   ;; Else error (unless NO-ERROR is non-nil, then FILE is returned unchanged)
  1103.   ;;DIR defaults to default-directory."
  1104.   ;; DIR must be file-name-as-directory, as with all directory args in
  1105.   ;; Emacs Lisp code.
  1106.   (or dir (setq dir default-directory))
  1107.   ;; This case comes into play if default-directory is set to
  1108.   ;; use ~.
  1109.   (if (and (> (length dir) 0) (= (aref dir 0) ?~))
  1110.       (setq dir (expand-file-name dir)))
  1111.   (if (string-match (concat "^" (regexp-quote dir)) file)
  1112.       (substring file (match-end 0))
  1113.     (if no-error
  1114.     file
  1115.       (error "%s: not in directory tree growing at %s" file dir))))
  1116.  
  1117. ;;; Functions for finding the file name in a dired buffer line.
  1118.  
  1119. ;; Move to first char of filename on this line.
  1120. ;; Returns position (point) or nil if no filename on this line."
  1121. (defun dired-move-to-filename (&optional raise-error eol)
  1122.   ;; This is the UNIX version.
  1123.   (or eol (setq eol (progn (end-of-line) (point))))
  1124.   (beginning-of-line)
  1125.   (if (re-search-forward
  1126.        "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)[ ]+[0-9]+"
  1127.        eol t)
  1128.       (progn
  1129.     (skip-chars-forward " ")    ; there is one SPC after day of month
  1130.     (skip-chars-forward "^ " eol)    ; move after time of day (or year)
  1131.     (skip-chars-forward " " eol)    ; there is space before the file name
  1132.     ;; Actually, if the year instead of clock time is displayed,
  1133.     ;; there are (only for some ls programs?) two spaces instead
  1134.     ;; of one before the name.
  1135.     ;; If we could depend on ls inserting exactly one SPC we
  1136.     ;; would not bomb on names _starting_ with SPC.
  1137.     (point))
  1138.     (if raise-error
  1139.     (error "No file on this line")
  1140.       nil)))
  1141.  
  1142. (defun dired-move-to-end-of-filename (&optional no-error)
  1143.   ;; Assumes point is at beginning of filename,
  1144.   ;; thus the rwx bit re-search-backward below will succeed in *this*
  1145.   ;; line if at all.  So, it should be called only after
  1146.   ;; (dired-move-to-filename t).
  1147.   ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
  1148.   ;; This is the UNIX version.
  1149.   (let (opoint file-type executable symlink hidden case-fold-search used-F eol)
  1150.     ;; case-fold-search is nil now, so we can test for capital F:
  1151.     (setq used-F (string-match "F" dired-actual-switches)
  1152.       opoint (point)
  1153.           eol (save-excursion (end-of-line) (point))
  1154.       hidden (and selective-display
  1155.               (save-excursion (search-forward "\r" eol t))))
  1156.     (if hidden
  1157.     nil
  1158.       (save-excursion;; Find out what kind of file this is:
  1159.     ;; Restrict perm bits to be non-blank,
  1160.     ;; otherwise this matches one char to early (looking backward):
  1161.     ;; "l---------" (some systems make symlinks that way)
  1162.     ;; "----------" (plain file with zero perms)
  1163.     (if (re-search-backward
  1164.          "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
  1165.          nil t)
  1166.         (setq file-type (char-after (match-beginning 1))
  1167.           symlink (eq file-type ?l)
  1168.           ;; Only with -F we need to know whether it's an executable
  1169.           executable (and
  1170.                   used-F
  1171.                   (string-match
  1172.                    "[xst]";; execute bit set anywhere?
  1173.                    (concat
  1174.                 (buffer-substring (match-beginning 2)
  1175.                           (match-end 2))
  1176.                 (buffer-substring (match-beginning 3)
  1177.                           (match-end 3))
  1178.                 (buffer-substring (match-beginning 4)
  1179.                           (match-end 4))))))
  1180.       (or no-error (error "No file on this line"))))
  1181.       ;; Move point to end of name:
  1182.       (if symlink
  1183.       (if (search-forward " ->" eol t)
  1184.           (progn
  1185.         (forward-char -3)
  1186.         (and used-F
  1187.              dired-ls-F-marks-symlinks
  1188.              (eq (preceding-char) ?@);; did ls really mark the link?
  1189.              (forward-char -1))))
  1190.     (goto-char eol);; else not a symbolic link
  1191.     ;; ls -lF marks dirs, sockets and executables with exactly one
  1192.     ;; trailing character. (Executable bits on symlinks ain't mean
  1193.     ;; a thing, even to ls, but we know it's not a symlink.)
  1194.     (and used-F
  1195.          (or (memq file-type '(?d ?s))
  1196.          executable)
  1197.          (forward-char -1))))
  1198.     (or no-error
  1199.     (not (eq opoint (point)))
  1200.     (error (if hidden
  1201.            (substitute-command-keys
  1202.             "File line is hidden, type \\[dired-hide-subdir] to unhide")
  1203.          "No file on this line")))
  1204.     (if (eq opoint (point))
  1205.     nil
  1206.       (point))))
  1207.  
  1208.  
  1209. ;; Keeping Dired buffers in sync with the filesystem and with each other
  1210.  
  1211. (defvar dired-buffers nil
  1212.   ;; Enlarged by dired-advertise
  1213.   ;; Queried by function dired-buffers-for-dir. When this detects a
  1214.   ;; killed buffer, it is removed from this list.
  1215.   "Alist of directories and their associated dired buffers.")
  1216.  
  1217. (defun dired-buffers-for-dir (dir)
  1218. ;; Return a list of buffers that dired DIR (top level or in-situ subdir).
  1219. ;; The list is in reverse order of buffer creation, most recent last.
  1220. ;; As a side effect, killed dired buffers for DIR are removed from
  1221. ;; dired-buffers.
  1222.   (setq dir (file-name-as-directory dir))
  1223.   (let ((alist dired-buffers) result elt)
  1224.     (while alist
  1225.       (setq elt (car alist))
  1226.       (if (dired-in-this-tree dir (car elt))
  1227.       (let ((buf (cdr elt)))
  1228.         (if (buffer-name buf)
  1229.         (if (assoc dir (save-excursion
  1230.                  (set-buffer buf)
  1231.                  dired-subdir-alist))
  1232.             (setq result (cons buf result)))
  1233.           ;; else buffer is killed - clean up:
  1234.           (setq dired-buffers (delq elt dired-buffers)))))
  1235.       (setq alist (cdr alist)))
  1236.     result))
  1237.  
  1238. (defun dired-advertise ()
  1239.   ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
  1240.   ;; With wildcards we actually advertise too much.
  1241.   (if (memq (current-buffer) (dired-buffers-for-dir default-directory))
  1242.       t                    ; we have already advertised ourselves
  1243.     (setq dired-buffers
  1244.       (cons (cons default-directory (current-buffer))
  1245.         dired-buffers))))
  1246.  
  1247. (defun dired-unadvertise (dir)
  1248.   ;; Remove DIR from the buffer alist in variable dired-buffers.
  1249.   ;; This has the effect of removing any buffer whose main directory is DIR.
  1250.   ;; It does not affect buffers in which DIR is a subdir.
  1251.   ;; Removing is also done as a side-effect in dired-buffer-for-dir.
  1252.   (setq dired-buffers
  1253.       (delq (assoc dir dired-buffers) dired-buffers)))
  1254.  
  1255. ;; Tree Dired
  1256.  
  1257. ;;; utility functions
  1258.  
  1259. (defun dired-in-this-tree (file dir)
  1260.   ;;"Is FILE part of the directory tree starting at DIR?"
  1261.   (let (case-fold-search)
  1262.     (string-match (concat "^" (regexp-quote dir)) file)))
  1263.  
  1264. (defun dired-normalize-subdir (dir)
  1265.   ;; Prepend default-directory to DIR if relative path name.
  1266.   ;; dired-get-filename must be able to make a valid filename from a
  1267.   ;; file and its directory DIR.
  1268.   (file-name-as-directory
  1269.    (if (file-name-absolute-p dir)
  1270.        dir
  1271.      (expand-file-name dir default-directory))))
  1272.  
  1273. (defun dired-get-subdir ()
  1274.   ;;"Return the subdir name on this line, or nil if not on a headerline."
  1275.   ;; Look up in the alist whether this is a headerline.
  1276.   (save-excursion
  1277.     (let ((cur-dir (dired-current-directory)))
  1278.       (beginning-of-line)        ; alist stores b-o-l positions
  1279.       (and (zerop (- (point)
  1280.              (dired-get-subdir-min (assoc cur-dir
  1281.                           dired-subdir-alist))))
  1282.        cur-dir))))
  1283.  
  1284. ;(defun dired-get-subdir-min (elt)
  1285. ;  (cdr elt))
  1286. ;; can't use macro,  must be redefinable for other alist format in dired-nstd.
  1287. (defalias 'dired-get-subdir-min 'cdr)
  1288.  
  1289. (defun dired-get-subdir-max (elt)
  1290.   (save-excursion
  1291.     (goto-char (dired-get-subdir-min elt))
  1292.     (dired-subdir-max)))
  1293.  
  1294. (defun dired-clear-alist ()
  1295.   (while dired-subdir-alist
  1296.     (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
  1297.     (setq dired-subdir-alist (cdr dired-subdir-alist))))
  1298.  
  1299. (defun dired-subdir-index (dir)
  1300.   ;; Return an index into alist for use with nth
  1301.   ;; for the sake of subdir moving commands.
  1302.   (let (found (index 0) (alist dired-subdir-alist))
  1303.     (while alist
  1304.       (if (string= dir (car (car alist)))
  1305.       (setq alist nil found t)
  1306.     (setq alist (cdr alist) index (1+ index))))
  1307.     (if found index nil)))
  1308.  
  1309. (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
  1310.   "Go to next subdirectory, regardless of level."
  1311.   ;; Use 0 arg to go to this directory's header line.
  1312.   ;; NO-SKIP prevents moving to end of header line, returning whatever
  1313.   ;; position was found in dired-subdir-alist.
  1314.   (interactive "p")
  1315.   (let ((this-dir (dired-current-directory))
  1316.     pos index)
  1317.     ;; nth with negative arg does not return nil but the first element
  1318.     (setq index (- (dired-subdir-index this-dir) arg))
  1319.     (setq pos (if (>= index 0)
  1320.           (dired-get-subdir-min (nth index dired-subdir-alist))))
  1321.     (if pos
  1322.     (progn
  1323.       (goto-char pos)
  1324.       (or no-skip (skip-chars-forward "^\n\r"))
  1325.       (point))
  1326.       (if no-error-if-not-found
  1327.       nil                ; return nil if not found
  1328.     (error "%s directory" (if (> arg 0) "Last" "First"))))))
  1329.  
  1330. (defun dired-build-subdir-alist ()
  1331.   "Build `dired-subdir-alist' by parsing the buffer.
  1332. Returns the new value of the alist."
  1333.   (interactive)
  1334.   (dired-clear-alist)
  1335.   (save-excursion
  1336.     (let ((count 0)
  1337.       (buffer-read-only nil)
  1338.       new-dir-name)
  1339.       (goto-char (point-min))
  1340.       (setq dired-subdir-alist nil)
  1341.       (while (re-search-forward dired-subdir-regexp nil t)
  1342.     (save-excursion
  1343.       (goto-char (match-beginning 1))
  1344.       (setq new-dir-name
  1345.         (expand-file-name (buffer-substring (point) (match-end 1))))
  1346.       (delete-region (point) (match-end 1))
  1347.       (insert new-dir-name))
  1348.     (setq count (1+ count))
  1349.     (dired-alist-add-1 new-dir-name
  1350.                ;; Place a sub directory boundary between lines.
  1351.                (save-excursion
  1352.                  (goto-char (match-beginning 0))
  1353.                  (beginning-of-line)
  1354.                  (point-marker))))
  1355.       (if (> count 1)
  1356.       (message "Buffer includes %d directories" count))
  1357.       ;; We don't need to sort it because it is in buffer order per
  1358.       ;; constructionem.  Return new alist:
  1359.       dired-subdir-alist)))
  1360.  
  1361. (defun dired-alist-add-1 (dir new-marker)
  1362.   ;; Add new DIR at NEW-MARKER.  Don't sort.
  1363.   (setq dired-subdir-alist
  1364.     (cons (cons (dired-normalize-subdir dir) new-marker)
  1365.           dired-subdir-alist)))
  1366.  
  1367. (defun dired-goto-next-nontrivial-file ()
  1368.   ;; Position point on first nontrivial file after point.
  1369.   (dired-goto-next-file);; so there is a file to compare with
  1370.   (if (stringp dired-trivial-filenames)
  1371.       (while (and (not (eobp))
  1372.           (string-match dired-trivial-filenames
  1373.                 (file-name-nondirectory
  1374.                  (or (dired-get-filename nil t) ""))))
  1375.     (forward-line 1)
  1376.     (dired-move-to-filename))))
  1377.  
  1378. (defun dired-goto-next-file ()
  1379.   (let ((max (1- (dired-subdir-max))))
  1380.     (while (and (not (dired-move-to-filename)) (< (point) max))
  1381.       (forward-line 1))))
  1382.  
  1383. (defun dired-goto-file (file)
  1384.   "Go to file line of FILE in this dired buffer."
  1385.   ;; Return value of point on success, else nil.
  1386.   ;; FILE must be an absolute pathname.
  1387.   ;; Loses if FILE contains control chars like "\007" for which ls
  1388.   ;; either inserts "?" or "\\007" into the buffer, so we won't find
  1389.   ;; it in the buffer.
  1390.   (interactive
  1391.    (prog1                ; let push-mark display its message
  1392.        (list (expand-file-name
  1393.           (read-file-name "Goto file: "
  1394.                   (dired-current-directory))))
  1395.      (push-mark)))
  1396.   (setq file (directory-file-name file)) ; does no harm if no directory
  1397.   (let (found case-fold-search dir)
  1398.     (setq dir (or (file-name-directory file)
  1399.           (error "Need absolute pathname for %s" file)))
  1400.     (save-excursion
  1401.       ;; The hair here is to get the result of dired-goto-subdir
  1402.       ;; without really calling it if we don't have any subdirs.
  1403.       (if (if (string= dir default-directory)
  1404.           (goto-char (point-min))
  1405.         (and (cdr dired-subdir-alist)
  1406.          (dired-goto-subdir dir)))
  1407.       (let ((base (file-name-nondirectory file))
  1408.         (boundary (dired-subdir-max)))
  1409.         (while (and (not found)
  1410.             ;; filenames are preceded by SPC, this makes
  1411.             ;; the search faster (e.g. for the filename "-"!).
  1412.              (search-forward (concat " " base) boundary 'move))
  1413.           ;; Match could have BASE just as initial substring or
  1414.           ;; or in permission bits or date or
  1415.           ;; not be a proper filename at all:
  1416.           (if (equal base (dired-get-filename 'no-dir t))
  1417.             ;; Must move to filename since an (actually
  1418.             ;; correct) match could have been elsewhere on the
  1419.             ;; ;; line (e.g. "-" would match somewhere in the
  1420.             ;; permission bits).
  1421.           (setq found (dired-move-to-filename))
  1422.         ;; If this isn't the right line, move forward to avoid
  1423.         ;; trying this line again.
  1424.         (forward-line 1))))))
  1425.     (and found
  1426.      ;; return value of point (i.e., FOUND):
  1427.      (goto-char found))))
  1428.  
  1429. (defun dired-initial-position (dirname)
  1430.   ;; Where point should go in a new listing of DIRNAME.
  1431.   ;; Point assumed at beginning of new subdir line.
  1432.   ;; You may redefine this function as you wish, e.g. like in dired-x.el.
  1433.   (end-of-line)
  1434.   (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
  1435.  
  1436. ;; These are hooks which make tree dired work.
  1437. ;; They are in this file because other parts of dired need to call them.
  1438. ;; But they don't call the rest of tree dired unless there are subdirs loaded.
  1439.  
  1440. ;; This function is called for each retrieved filename.
  1441. ;; It could stand to be faster, though it's mostly function call
  1442. ;; overhead.  Avoiding the function call seems to save about 10% in
  1443. ;; dired-get-filename.  Make it a defsubst?
  1444. (defun dired-current-directory (&optional localp)
  1445.   "Return the name of the subdirectory to which this line belongs.
  1446. This returns a string with trailing slash, like `default-directory'.
  1447. Optional argument means return a file name relative to `default-directory'."
  1448.   (let ((here (point))
  1449.     (alist (or dired-subdir-alist
  1450.            ;; probably because called in a non-dired buffer
  1451.            (error "No subdir-alist in %s" (current-buffer))))
  1452.     elt dir)
  1453.     (while alist
  1454.       (setq elt (car alist)
  1455.         dir (car elt)
  1456.         ;; use `<=' (not `<') as subdir line is part of subdir
  1457.         alist (if (<= (dired-get-subdir-min elt) here)
  1458.               nil        ; found
  1459.             (cdr alist))))
  1460.     (if localp
  1461.     (dired-make-relative dir default-directory)
  1462.       dir)))
  1463.  
  1464. ;; Subdirs start at the beginning of their header lines and end just
  1465. ;; before the beginning of the next header line (or end of buffer).
  1466.  
  1467. (defun dired-subdir-max ()
  1468.   (save-excursion
  1469.     (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
  1470.     (point-max)
  1471.       (point))))
  1472.  
  1473. ;; Deleting files
  1474.  
  1475. (defun dired-do-flagged-delete ()
  1476.   "In dired, delete the files flagged for deletion."
  1477.   (interactive)
  1478.   (let* ((dired-marker-char dired-del-marker)
  1479.      (regexp (dired-marker-regexp))
  1480.      case-fold-search)
  1481.     (if (save-excursion (goto-char (point-min))
  1482.             (re-search-forward regexp nil t))
  1483.     (dired-internal-do-deletions
  1484.      ;; this can't move point since ARG is nil
  1485.      (dired-map-over-marks (cons (dired-get-filename) (point))
  1486.                    nil)
  1487.      nil)
  1488.       (message "(No deletions requested)"))))
  1489.  
  1490. (defun dired-do-delete (&optional arg)
  1491.   "Delete all marked (or next ARG) files."
  1492.   ;; This is more consistent with the file marking feature than
  1493.   ;; dired-do-flagged-delete.
  1494.   (interactive "P")
  1495.   (dired-internal-do-deletions
  1496.    ;; this may move point if ARG is an integer
  1497.    (dired-map-over-marks (cons (dired-get-filename) (point))
  1498.              arg)
  1499.    arg))
  1500.  
  1501. (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
  1502.  
  1503. (defun dired-internal-do-deletions (l arg)
  1504.   ;; L is an alist of files to delete, with their buffer positions.
  1505.   ;; ARG is the prefix arg.
  1506.   ;; Filenames are absolute (VMS needs this for logical search paths).
  1507.   ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
  1508.   ;; That way as changes are made in the buffer they do not shift the
  1509.   ;; lines still to be changed, so the (point) values in L stay valid.
  1510.   ;; Also, for subdirs in natural order, a subdir's files are deleted
  1511.   ;; before the subdir itself - the other way around would not work.
  1512.   (let ((files (mapcar (function car) l))
  1513.     (count (length l))
  1514.     (succ 0))
  1515.     ;; canonicalize file list for pop up
  1516.     (setq files (nreverse (mapcar (function dired-make-relative) files)))
  1517.     (if (dired-mark-pop-up
  1518.      " *Deletions*" 'delete files dired-deletion-confirmer
  1519.      (format "Delete %s " (dired-mark-prompt arg files)))
  1520.     (save-excursion
  1521.       (let (failures);; files better be in reverse order for this loop!
  1522.         (while l
  1523.           (goto-char (cdr (car l)))
  1524.           (let (buffer-read-only)
  1525.         (condition-case err
  1526.             (let ((fn (car (car l))))
  1527.               ;; This test is equivalent to
  1528.               ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
  1529.               ;; but more efficient
  1530.               (if (eq t (car (file-attributes fn)))
  1531.               (delete-directory fn)
  1532.             (delete-file fn))
  1533.               ;; if we get here, removing worked
  1534.               (setq succ (1+ succ))
  1535.               (message "%s of %s deletions" succ count)
  1536.               (delete-region (progn (beginning-of-line) (point))
  1537.                      (progn (forward-line 1) (point)))
  1538.               (dired-clean-up-after-deletion fn))
  1539.           (error;; catch errors from failed deletions
  1540.            (dired-log "%s\n" err)
  1541.            (setq failures (cons (car (car l)) failures)))))
  1542.           (setq l (cdr l)))
  1543.         (if (not failures)
  1544.         (message "%d deletion%s done" count (dired-plural-s count))
  1545.           (dired-log-summary
  1546.            (format "%d of %d deletion%s failed"
  1547.                (length failures) count
  1548.                (dired-plural-s count))
  1549.            failures))))
  1550.       (message "(No deletions performed)")))
  1551.   (dired-move-to-filename))
  1552.  
  1553. ;; This is a separate function for the sake of dired-x.el.
  1554. (defun dired-clean-up-after-deletion (fn)
  1555.   ;; Clean up after a deleted file or directory FN.
  1556.   (save-excursion (and (cdr dired-subdir-alist)
  1557.                (dired-goto-subdir fn)
  1558.                (dired-kill-subdir))))
  1559.  
  1560. ;; Confirmation
  1561.  
  1562. (defun dired-marker-regexp ()
  1563.   (concat "^" (regexp-quote (char-to-string dired-marker-char))))
  1564.  
  1565. (defun dired-plural-s (count)
  1566.   (if (= 1 count) "" "s"))
  1567.  
  1568. (defun dired-mark-prompt (arg files)
  1569.   ;; Return a string for use in a prompt, either the current file
  1570.   ;; name, or the marker and a count of marked files.
  1571.   (let ((count (length files)))
  1572.     (if (= count 1)
  1573.     (car files)
  1574.       ;; more than 1 file:
  1575.       (if (integerp arg)
  1576.       ;; abs(arg) = count
  1577.       ;; Perhaps this is nicer, but it also takes more screen space:
  1578.       ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
  1579.       ;;                        count)
  1580.       (format "[next %d files]" arg)
  1581.     (format "%c [%d files]" dired-marker-char count)))))
  1582.  
  1583. (defun dired-pop-to-buffer (buf)
  1584.   ;; Pop up buffer BUF.
  1585.   ;; If dired-shrink-to-fit is t, make its window fit its contents.
  1586.   (if (not dired-shrink-to-fit)
  1587.       (pop-to-buffer (get-buffer-create buf))
  1588.     ;; let window shrink to fit:
  1589.     (let ((window (selected-window))
  1590.       target-lines w2)
  1591.       (cond ;; if split-window-threshold is enabled, use the largest window
  1592.             ((and (> (window-height (setq w2 (get-largest-window)))
  1593.              split-height-threshold)
  1594.           (= (frame-width) (window-width w2)))
  1595.          (setq window w2))
  1596.         ;; if the least-recently-used window is big enough, use it
  1597.         ((and (> (window-height (setq w2 (get-lru-window)))
  1598.              (* 2 window-min-height))
  1599.           (= (frame-width) (window-width w2)))
  1600.          (setq window w2)))
  1601.       (save-excursion
  1602.     (set-buffer buf)
  1603.     (goto-char (point-max))
  1604.     (skip-chars-backward "\n\r\t ")
  1605.     (setq target-lines (count-lines (point-min) (point)))
  1606.     ;; Don't forget to count the last line.
  1607.     (if (not (bolp))
  1608.         (setq target-lines (1+ target-lines))))
  1609.       (if (<= (window-height window) (* 2 window-min-height))
  1610.       ;; At this point, every window on the frame is too small to split.
  1611.       (setq w2 (display-buffer buf))
  1612.     (setq w2 (split-window window
  1613.           (max window-min-height
  1614.                (- (window-height window)
  1615.               (1+ (max window-min-height target-lines)))))))
  1616.       (set-window-buffer w2 buf)
  1617.       (if (< (1- (window-height w2)) target-lines)
  1618.       (progn
  1619.         (select-window w2)
  1620.         (enlarge-window (- target-lines (1- (window-height w2))))))
  1621.       (set-window-start w2 1)
  1622.       )))
  1623.  
  1624. (defvar dired-no-confirm nil
  1625. ;;  "If non-nil, list of symbols for commands dired should not confirm.
  1626. ;;It can be a sublist of
  1627. ;;
  1628. ;;  '(byte-compile chgrp chmod chown compress copy delete hardlink load
  1629. ;;    move print shell symlink uncompress)"
  1630.   )
  1631.  
  1632. (defun dired-mark-pop-up (bufname op-symbol files function &rest args)
  1633.   ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
  1634.   ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer
  1635.   ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked
  1636.   ;;files.  Uses function `dired-pop-to-buffer' to do that.
  1637.   ;; FUNCTION should not manipulate files.
  1638.   ;; It should only read input (an argument or confirmation).
  1639.   ;;The window is not shown if there is just one file or
  1640.   ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
  1641.   ;;FILES is the list of marked files."
  1642.   (or bufname (setq bufname  " *Marked Files*"))
  1643.   (if (or (memq op-symbol dired-no-confirm)
  1644.       (= (length files) 1))
  1645.       (apply function args)
  1646.     (save-excursion
  1647.       (set-buffer (get-buffer-create bufname))
  1648.       (erase-buffer)
  1649.       (dired-format-columns-of-files files))
  1650.     (save-window-excursion
  1651.       (dired-pop-to-buffer bufname)
  1652.       (apply function args))))
  1653.  
  1654. (defun dired-format-columns-of-files (files)
  1655.   ;; Files should be in forward order for this loop.
  1656.   ;; i.e., (car files) = first file in buffer.
  1657.   ;; Returns the number of lines used.
  1658.   (let* ((maxlen (+ 2 (apply 'max (mapcar 'length files))))
  1659.      (width (- (window-width (selected-window)) 2))
  1660.      (columns (max 1 (/ width maxlen)))
  1661.      (nfiles (length files))
  1662.      (rows (+ (/ nfiles columns)
  1663.           (if (zerop (% nfiles columns)) 0 1)))
  1664.      (i 0)
  1665.      (j 0))
  1666.     (setq files (nconc (copy-sequence files) ; fill up with empty fns
  1667.                (make-list (- (* columns rows) nfiles) "")))
  1668.     (setcdr (nthcdr (1- (length files)) files) files) ; make circular
  1669.     (while (< j rows)
  1670.       (while (< i columns)
  1671.     (indent-to (* i maxlen))
  1672.     (insert (car files))
  1673.     (setq files (nthcdr rows files)
  1674.           i (1+ i)))
  1675.       (insert "\n")
  1676.       (setq i 0
  1677.         j (1+ j)
  1678.         files (cdr files)))
  1679.     rows))
  1680.  
  1681. ;; Commands to mark or flag file(s) at or near current line.
  1682.  
  1683. (defun dired-repeat-over-lines (arg function)
  1684.   ;; This version skips non-file lines.
  1685.   (let ((pos (make-marker)))
  1686.     (beginning-of-line)
  1687.     (while (and (> arg 0) (not (eobp)))
  1688.       (setq arg (1- arg))
  1689.       (beginning-of-line)
  1690.       (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
  1691.       (save-excursion
  1692.     (forward-line 1)
  1693.     (move-marker pos (1+ (point))))
  1694.       (save-excursion (funcall function))
  1695.       ;; Advance to the next line--actually, to the line that *was* next.
  1696.       ;; (If FUNCTION inserted some new lines in between, skip them.)
  1697.       (goto-char pos))
  1698.     (while (and (< arg 0) (not (bobp)))
  1699.       (setq arg (1+ arg))
  1700.       (forward-line -1)
  1701.       (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
  1702.       (beginning-of-line)
  1703.       (save-excursion (funcall function)))
  1704.     (move-marker pos nil)
  1705.     (dired-move-to-filename)))
  1706.  
  1707. (defun dired-between-files ()
  1708.   ;; Point must be at beginning of line
  1709.   ;; Should be equivalent to (save-excursion (not (dired-move-to-filename)))
  1710.   ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it)
  1711.   (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard")
  1712.       (looking-at dired-subdir-regexp)))
  1713.  
  1714. (defun dired-next-marked-file (arg &optional wrap opoint)
  1715.   "Move to the next marked file, wrapping around the end of the buffer."
  1716.   (interactive "p\np")
  1717.   (or opoint (setq opoint (point)));; return to where interactively started
  1718.   (if (if (> arg 0)
  1719.       (re-search-forward dired-re-mark nil t arg)
  1720.     (beginning-of-line)
  1721.     (re-search-backward dired-re-mark nil t (- arg)))
  1722.       (dired-move-to-filename)
  1723.     (if (null wrap)
  1724.     (progn
  1725.       (goto-char opoint)
  1726.       (error "No next marked file"))
  1727.       (message "(Wraparound for next marked file)")
  1728.       (goto-char (if (> arg 0) (point-min) (point-max)))
  1729.       (dired-next-marked-file arg nil opoint))))
  1730.  
  1731. (defun dired-prev-marked-file (arg &optional wrap)
  1732.   "Move to the previous marked file, wrapping around the end of the buffer."
  1733.   (interactive "p\np")
  1734.   (dired-next-marked-file (- arg) wrap))
  1735.  
  1736. (defun dired-file-marker (file)
  1737.   ;; Return FILE's marker, or nil if unmarked.
  1738.   (save-excursion
  1739.     (and (dired-goto-file file)
  1740.      (progn
  1741.        (beginning-of-line)
  1742.        (if (not (equal ?\040 (following-char)))
  1743.            (following-char))))))
  1744.  
  1745. (defun dired-mark-files-in-region (start end)
  1746.   (let (buffer-read-only)
  1747.     (if (> start end)
  1748.     (error "start > end"))
  1749.     (goto-char start)            ; assumed at beginning of line
  1750.     (while (< (point) end)
  1751.       ;; Skip subdir line and following garbage like the `total' line:
  1752.       (while (and (< (point) end) (dired-between-files))
  1753.     (forward-line 1))
  1754.       (if (and (not (looking-at dired-re-dot))
  1755.            (dired-get-filename nil t))
  1756.       (progn
  1757.         (delete-char 1)
  1758.         (insert dired-marker-char)))
  1759.       (forward-line 1))))
  1760.  
  1761. (defun dired-mark (arg)
  1762.   "Mark the current (or next ARG) files.
  1763. If on a subdir headerline, mark all its files except `.' and `..'.
  1764.  
  1765. Use \\[dired-unmark-all-files] to remove all marks
  1766. and \\[dired-unmark] on a subdir to remove the marks in
  1767. this subdir."
  1768.   (interactive "P")
  1769.   (if (and (cdr dired-subdir-alist) (dired-get-subdir))
  1770.       (save-excursion (dired-mark-subdir-files))
  1771.     (let (buffer-read-only)
  1772.       (dired-repeat-over-lines
  1773.        (prefix-numeric-value arg)
  1774.        (function (lambda () (delete-char 1) (insert dired-marker-char)))))))
  1775.  
  1776. (defun dired-unmark (arg)
  1777.   "Unmark the current (or next ARG) files.
  1778. If looking at a subdir, unmark all its files except `.' and `..'."
  1779.   (interactive "P")
  1780.   (let ((dired-marker-char ?\040))
  1781.     (dired-mark arg)))
  1782.  
  1783. (defun dired-flag-file-deletion (arg)
  1784.   "In dired, flag the current line's file for deletion.
  1785. With prefix arg, repeat over several lines.
  1786.  
  1787. If on a subdir headerline, mark all its files except `.' and `..'."
  1788.   (interactive "P")
  1789.   (let ((dired-marker-char dired-del-marker))
  1790.     (dired-mark arg)))
  1791.  
  1792. (defun dired-unmark-backward (arg)
  1793.   "In dired, move up lines and remove deletion flag there.
  1794. Optional prefix ARG says how many lines to unflag; default is one line."
  1795.   (interactive "p")
  1796.   (dired-unmark (- arg)))
  1797.  
  1798. ;;; Commands to mark or flag files based on their characteristics or names.
  1799.  
  1800. (defvar dired-regexp-history nil
  1801.   "History list of regular expressions used in Dired commands.")
  1802.  
  1803. (defun dired-read-regexp (prompt)
  1804.   (read-from-minibuffer prompt nil nil nil 'dired-regexp-history))
  1805.  
  1806. (defun dired-mark-files-regexp (regexp &optional marker-char)
  1807.   "Mark all files matching REGEXP for use in later commands.
  1808. A prefix argument means to unmark them instead.
  1809. `.' and `..' are never marked.
  1810.  
  1811. REGEXP is an Emacs regexp, not a shell wildcard.  Thus, use `\\.o$' for
  1812. object files--just `.o' will mark more than you might think."
  1813.   (interactive
  1814.    (list (dired-read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
  1815.                     " files (regexp): "))
  1816.      (if current-prefix-arg ?\040)))
  1817.   (let ((dired-marker-char (or marker-char dired-marker-char)))
  1818.     (dired-mark-if
  1819.      (and (not (looking-at dired-re-dot))
  1820.       (not (eolp))            ; empty line
  1821.       (let ((fn (dired-get-filename nil t)))
  1822.         (and fn (string-match regexp (file-name-nondirectory fn)))))
  1823.      "matching file")))
  1824.  
  1825. (defun dired-flag-files-regexp (regexp)
  1826.   "In dired, flag all files containing the specified REGEXP for deletion.
  1827. The match is against the non-directory part of the filename.  Use `^'
  1828.   and `$' to anchor matches.  Exclude subdirs by hiding them.
  1829. `.' and `..' are never flagged."
  1830.   (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
  1831.   (dired-mark-files-regexp regexp dired-del-marker))
  1832.  
  1833. (defun dired-mark-symlinks (unflag-p)
  1834.   "Mark all symbolic links.
  1835. With prefix argument, unflag all those files."
  1836.   (interactive "P")
  1837.   (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
  1838.     (dired-mark-if (looking-at dired-re-sym) "symbolic link")))
  1839.  
  1840. (defun dired-mark-directories (unflag-p)
  1841.   "Mark all directory file lines except `.' and `..'.
  1842. With prefix argument, unflag all those files."
  1843.   (interactive "P")
  1844.   (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
  1845.     (dired-mark-if (and (looking-at dired-re-dir)
  1846.             (not (looking-at dired-re-dot)))
  1847.            "directory file")))
  1848.  
  1849. (defun dired-mark-executables (unflag-p)
  1850.   "Mark all executable files.
  1851. With prefix argument, unflag all those files."
  1852.   (interactive "P")
  1853.   (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
  1854.     (dired-mark-if (looking-at dired-re-exe) "executable file")))
  1855.  
  1856. ;; dired-x.el has a dired-mark-sexp interactive command: mark
  1857. ;; files for which PREDICATE returns non-nil.
  1858.  
  1859. (defun dired-flag-auto-save-files (&optional unflag-p)
  1860.   "Flag for deletion files whose names suggest they are auto save files.
  1861. A prefix argument says to unflag those files instead."
  1862.   (interactive "P")
  1863.   (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
  1864.     (dired-mark-if
  1865.      ;; It is less than general to check for # here,
  1866.      ;; but it's the only way this runs fast enough.
  1867.      (and (save-excursion (end-of-line)
  1868.                           (or
  1869.                            (eq (preceding-char) ?#)
  1870.                            ;; Handle executables in case of -F option.
  1871.                            ;; We need not worry about the other kinds
  1872.                            ;; of markings that -F makes, since they won't
  1873.                            ;; appear on real auto-save files.
  1874.                            (if (eq (preceding-char) ?*)
  1875.                                (progn
  1876.                                  (forward-char -1)
  1877.                                  (eq (preceding-char) ?#)))))
  1878.       (not (looking-at dired-re-dir))
  1879.       (let ((fn (dired-get-filename t t)))
  1880.         (if fn (auto-save-file-name-p
  1881.             (file-name-nondirectory fn)))))
  1882.      "auto save file")))
  1883.  
  1884. (defun dired-flag-backup-files (&optional unflag-p)
  1885.   "Flag all backup files (names ending with `~') for deletion.
  1886. With prefix argument, unflag these files."
  1887.   (interactive "P")
  1888.   (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
  1889.     (dired-mark-if
  1890.      ;; It is less than general to check for ~ here,
  1891.      ;; but it's the only way this runs fast enough.
  1892.      (and (save-excursion (end-of-line)
  1893.               (or
  1894.                (eq (preceding-char) ?~)
  1895.                ;; Handle executables in case of -F option.
  1896.                ;; We need not worry about the other kinds
  1897.                ;; of markings that -F makes, since they won't
  1898.                ;; appear on real backup files.
  1899.                (if (eq (preceding-char) ?*)
  1900.                    (progn
  1901.                  (forward-char -1)
  1902.                  (eq (preceding-char) ?~)))))
  1903.       (not (looking-at dired-re-dir))
  1904.       (let ((fn (dired-get-filename t t)))
  1905.         (if fn (backup-file-name-p fn))))
  1906.      "backup file")))
  1907.  
  1908. (defun dired-change-marks (&optional old new)
  1909.   "Change all OLD marks to NEW marks.
  1910. OLD and NEW are both characters used to mark files."
  1911.   (interactive
  1912.    (let* ((cursor-in-echo-area t)
  1913.       (old (progn (message "Change (old mark): ") (read-char)))
  1914.       (new (progn (message  "Change %c marks to (new mark): " old)
  1915.               (read-char))))
  1916.      (list old new)))
  1917.   (if (or (eq old ?\r) (eq new ?\r))
  1918.       (ding)
  1919.     (let ((string (format "\n%c" old))
  1920.       (buffer-read-only))
  1921.       (save-excursion
  1922.     (goto-char (point-min))
  1923.     (while (search-forward string nil t)
  1924.       (subst-char-in-region (match-beginning 0)
  1925.                 (match-end 0) old new))))))
  1926.  
  1927. (defun dired-unmark-all-files-no-query ()
  1928.   "Remove all marks from all files in the Dired buffer."
  1929.   (interactive)
  1930.   (dired-unmark-all-files ?\r))
  1931.  
  1932. (defun dired-unmark-all-files (mark &optional arg)
  1933.   "Remove a specific mark (or any mark) from every file.
  1934. After this command, type the mark character to remove, 
  1935. or type RET to remove all marks.
  1936. With prefix arg, query for each marked file.
  1937. Type \\[help-command] at that time for help."
  1938.   (interactive "cRemove marks (RET means all): \nP")
  1939.   (save-excursion
  1940.     (let* ((count 0)
  1941.        buffer-read-only case-fold-search query
  1942.        (string (format "\n%c" mark))
  1943.        (help-form "\
  1944. Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
  1945. `!' to unmark all remaining files with no more questions."))
  1946.       (goto-char (point-min))
  1947.       (while (if (eq mark ?\r)
  1948.          (re-search-forward dired-re-mark nil t)
  1949.            (search-forward string nil t))
  1950.     (if (or (not arg)
  1951.         (dired-query 'query "Unmark file `%s'? "
  1952.                  (dired-get-filename t)))
  1953.         (progn (subst-char-in-region (1- (point)) (point)
  1954.                      (preceding-char) ?\ )
  1955.            (setq count (1+ count)))))
  1956.       (message (if (= count 1) "1 mark removed"
  1957.          "%d marks removed")
  1958.            count))))
  1959.  
  1960. ;; Logging failures operating on files, and showing the results.
  1961.  
  1962. (defvar dired-log-buffer "*Dired log*")
  1963.  
  1964. (defun dired-why ()
  1965.   "Pop up a buffer with error log output from Dired.
  1966. A group of errors from a single command ends with a formfeed.
  1967. Thus, use \\[backward-page] to find the beginning of a group of errors."
  1968.   (interactive)
  1969.   (if (get-buffer dired-log-buffer)
  1970.       (let ((owindow (selected-window))
  1971.         (window (display-buffer (get-buffer dired-log-buffer))))
  1972.     (unwind-protect
  1973.         (progn
  1974.           (select-window window)
  1975.           (goto-char (point-max))
  1976.           (recenter -1))
  1977.       (select-window owindow)))))
  1978.  
  1979. (defun dired-log (log &rest args)
  1980.   ;; Log a message or the contents of a buffer.
  1981.   ;; If LOG is a string and there are more args, it is formatted with
  1982.   ;; those ARGS.  Usually the LOG string ends with a \n.
  1983.   ;; End each bunch of errors with (dired-log t): this inserts
  1984.   ;; current time and buffer, and a \f (formfeed).
  1985.   (let ((obuf (current-buffer)))
  1986.     (unwind-protect            ; want to move point
  1987.     (progn
  1988.       (set-buffer (get-buffer-create dired-log-buffer))
  1989.       (goto-char (point-max))
  1990.       (let (buffer-read-only)
  1991.         (cond ((stringp log)
  1992.            (insert (if args
  1993.                    (apply (function format) log args)
  1994.                  log)))
  1995.           ((bufferp log)
  1996.            (insert-buffer log))
  1997.           ((eq t log)
  1998.            (insert "\n\t" (current-time-string)
  1999.                "\tBuffer `" (buffer-name obuf) "'\n\f\n")))))
  2000.       (set-buffer obuf))))
  2001.  
  2002. (defun dired-log-summary (string failures)
  2003.   (message (if failures "%s--type ? for details (%s)"
  2004.          "%s--type ? for details")
  2005.        string failures)
  2006.   ;; Log a summary describing a bunch of errors.
  2007.   (dired-log (concat "\n" string))
  2008.   (dired-log t))
  2009.  
  2010. ;;; Sorting
  2011.  
  2012. ;; Most ls can only sort by name or by date (with -t), nothing else.
  2013. ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
  2014. ;; So anything that does not contain these is sort "by name".
  2015.  
  2016. (defvar dired-ls-sorting-switches "SXU"
  2017.   "String of `ls' switches (single letters) except `t' that influence sorting.")
  2018.  
  2019. (defvar dired-sort-by-date-regexp
  2020.   (concat "^-[^" dired-ls-sorting-switches
  2021.       "]*t[^" dired-ls-sorting-switches "]*$")
  2022.   "Regexp recognized by dired to set `by date' mode.")
  2023.  
  2024. (defvar dired-sort-by-name-regexp
  2025.   (concat "^-[^t" dired-ls-sorting-switches "]+$")
  2026.   "Regexp recognized by dired to set `by name' mode.")
  2027.  
  2028. (defun dired-sort-set-modeline ()
  2029.   ;; Set modeline display according to dired-actual-switches.
  2030.   ;; Modeline display of "by name" or "by date" guarantees the user a
  2031.   ;; match with the corresponding regexps.  Non-matching switches are
  2032.   ;; shown literally.
  2033.   (setq mode-name
  2034.     (let (case-fold-search)
  2035.       (cond ((string-match dired-sort-by-name-regexp dired-actual-switches)
  2036.          "Dired by name")
  2037.         ((string-match dired-sort-by-date-regexp dired-actual-switches)
  2038.          "Dired by date")
  2039.         (t
  2040.          (concat "Dired " dired-actual-switches)))))
  2041.   ;; update mode line:
  2042.   (set-buffer-modified-p (buffer-modified-p)))
  2043.  
  2044. (defun dired-sort-toggle-or-edit (&optional arg)
  2045.   "Toggle between sort by date/name and refresh the dired buffer.
  2046. With a prefix argument you can edit the current listing switches instead."
  2047.   (interactive "P")
  2048.   (if arg
  2049.       (dired-sort-other
  2050.        (read-string "ls switches (must contain -l): " dired-actual-switches))
  2051.     (dired-sort-toggle)))
  2052.  
  2053. (defun dired-sort-toggle ()
  2054.   ;; Toggle between sort by date/name.  Reverts the buffer.
  2055.   (setq dired-actual-switches
  2056.     (let (case-fold-search)
  2057.       (concat
  2058.        "-l"
  2059.        (dired-replace-in-string (concat "[---lt"
  2060.                         dired-ls-sorting-switches "]")
  2061.                     ""
  2062.                     dired-actual-switches)
  2063.        (if (string-match (concat "[t" dired-ls-sorting-switches "]")
  2064.                  dired-actual-switches)
  2065.            ""
  2066.          "t"))))
  2067.   (dired-sort-set-modeline)
  2068.   (revert-buffer))
  2069.  
  2070. (defun dired-replace-in-string (regexp newtext string)
  2071.   ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
  2072.   ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
  2073.   (let ((result "") (start 0) mb me)
  2074.     (while (string-match regexp string start)
  2075.       (setq mb (match-beginning 0)
  2076.         me (match-end 0)
  2077.         result (concat result (substring string start mb) newtext)
  2078.         start me))
  2079.     (concat result (substring string start))))
  2080.  
  2081. (defun dired-sort-other (switches &optional no-revert)
  2082.   ;; Specify new ls SWITCHES for current dired buffer.  Values matching
  2083.   ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
  2084.   ;; minor mode accordingly, others appear literally in the mode line.
  2085.   ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
  2086.   (setq dired-actual-switches switches)
  2087.   (dired-sort-set-modeline)
  2088.   (or no-revert (revert-buffer)))
  2089.  
  2090. ;; To make this file smaller, the less common commands
  2091. ;; go in a separate file.  But autoload them here
  2092. ;; to make the separation invisible.
  2093.  
  2094. (autoload 'dired-diff "dired-aux"
  2095.   "Compare file at point with file FILE using `diff'.
  2096. FILE defaults to the file at the mark.
  2097. The prompted-for file is the first file given to `diff'."
  2098.   t)
  2099.  
  2100. (autoload 'dired-backup-diff "dired-aux"
  2101.   "Diff this file with its backup file or vice versa.
  2102. Uses the latest backup, if there are several numerical backups.
  2103. If this file is a backup, diff it with its original.
  2104. The backup file is the first file given to `diff'."
  2105.   t)
  2106.  
  2107. (autoload 'dired-clean-directory "dired-aux"
  2108.   "Flag numerical backups for deletion.
  2109. Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
  2110. Positive prefix arg KEEP overrides `dired-kept-versions';
  2111. Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
  2112.  
  2113. To clear the flags on these files, you can use \\[dired-flag-backup-files]
  2114. with a prefix argument."
  2115.   t)
  2116.  
  2117. (autoload 'dired-do-chmod "dired-aux"
  2118.   "Change the mode of the marked (or next ARG) files.
  2119. This calls chmod, thus symbolic modes like `g+w' are allowed."
  2120.   t)
  2121.  
  2122. (autoload 'dired-do-chgrp "dired-aux"
  2123.   "Change the group of the marked (or next ARG) files."
  2124.   t)
  2125.  
  2126. (autoload 'dired-do-chown "dired-aux"
  2127.   "Change the owner of the marked (or next ARG) files."
  2128.   t)
  2129.  
  2130. (autoload 'dired-do-print "dired-aux"
  2131.   "Print the marked (or next ARG) files.
  2132. Uses the shell command coming from variables `lpr-command' and
  2133. `lpr-switches' as default."
  2134.   t)
  2135.  
  2136. (autoload 'dired-do-shell-command "dired-aux"
  2137.   "Run a shell command COMMAND on the marked files.
  2138. If no files are marked or a specific numeric prefix arg is given,
  2139. the next ARG files are used.  Just \\[universal-argument] means the current file.
  2140. The prompt mentions the file(s) or the marker, as appropriate.
  2141.  
  2142. If there is output, it goes to a separate buffer.
  2143.  
  2144. Normally the command is run on each file individually.
  2145. However, if there is a `*' in the command then it is run
  2146. just once with the entire file list substituted there.
  2147.  
  2148. No automatic redisplay of dired buffers is attempted, as there's no
  2149. telling what files the command may have changed.  Type
  2150. \\[dired-do-redisplay] to redisplay the marked files.
  2151.  
  2152. The shell command has the top level directory as working directory, so
  2153. output files usually are created there instead of in a subdir."
  2154.   t)
  2155.  
  2156. (autoload 'dired-do-kill-lines "dired-aux"
  2157.   "Kill all marked lines (not the files).
  2158. With a prefix arg, kill all lines not marked or flagged."
  2159.   t)
  2160.  
  2161. (autoload 'dired-do-compress "dired-aux"
  2162.   "Compress or uncompress marked (or next ARG) files."
  2163.   t)
  2164.  
  2165. (autoload 'dired-do-byte-compile "dired-aux"
  2166.   "Byte compile marked (or next ARG) Emacs Lisp files."
  2167.   t)
  2168.  
  2169. (autoload 'dired-do-load "dired-aux"
  2170.   "Load the marked (or next ARG) Emacs Lisp files."
  2171.   t)
  2172.  
  2173. (autoload 'dired-do-redisplay "dired-aux"
  2174.   "Redisplay all marked (or next ARG) files.
  2175. If on a subdir line, redisplay that subdirectory.  In that case,
  2176. a prefix arg lets you edit the `ls' switches used for the new listing."
  2177.   t)
  2178.  
  2179. (autoload 'dired-string-replace-match "dired-aux"
  2180.   "Replace first match of REGEXP in STRING with NEWTEXT.
  2181. If it does not match, nil is returned instead of the new string.
  2182. Optional arg LITERAL means to take NEWTEXT literally.
  2183. Optional arg GLOBAL means to replace all matches."
  2184.   t)
  2185.  
  2186. (autoload 'dired-create-directory "dired-aux"
  2187.   "Create a directory called DIRECTORY."
  2188.   t)
  2189.  
  2190. (autoload 'dired-do-copy "dired-aux"
  2191.   "Copy all marked (or next ARG) files, or copy the current file.
  2192. Thus, a zero prefix argument copies nothing.  But it toggles the
  2193. variable `dired-copy-preserve-time' (which see)."
  2194.   t)
  2195.  
  2196. (autoload 'dired-do-symlink "dired-aux"
  2197.   "Make symbolic links to current file or all marked (or next ARG) files.
  2198. When operating on just the current file, you specify the new name.
  2199. When operating on multiple or marked files, you specify a directory
  2200. and new symbolic links are made in that directory
  2201. with the same names that the files currently have."
  2202.   t)
  2203.  
  2204. (autoload 'dired-do-hardlink "dired-aux"
  2205.   "Add names (hard links) current file or all marked (or next ARG) files.
  2206. When operating on just the current file, you specify the new name.
  2207. When operating on multiple or marked files, you specify a directory
  2208. and new hard links are made in that directory
  2209. with the same names that the files currently have."
  2210.   t)
  2211.  
  2212. (autoload 'dired-do-rename "dired-aux"
  2213.   "Rename current file or all marked (or next ARG) files.
  2214. When renaming just the current file, you specify the new name.
  2215. When renaming multiple or marked files, you specify a directory."
  2216.   t)
  2217.  
  2218. (autoload 'dired-do-rename-regexp "dired-aux"
  2219.   "Rename marked files containing REGEXP to NEWNAME.
  2220. As each match is found, the user must type a character saying
  2221.   what to do with it.  For directions, type \\[help-command] at that time.
  2222. NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
  2223. REGEXP defaults to the last regexp used.
  2224. With a zero prefix arg, renaming by regexp affects the complete
  2225.   pathname - usually only the non-directory part of file names is used
  2226.   and changed."
  2227.   t)
  2228.  
  2229. (autoload 'dired-do-copy-regexp "dired-aux"
  2230.   "Copy all marked files containing REGEXP to NEWNAME.
  2231. See function `dired-rename-regexp' for more info."
  2232.   t)
  2233.  
  2234. (autoload 'dired-do-hardlink-regexp "dired-aux"
  2235.   "Hardlink all marked files containing REGEXP to NEWNAME.
  2236. See function `dired-rename-regexp' for more info."
  2237.   t)
  2238.  
  2239. (autoload 'dired-do-symlink-regexp "dired-aux"
  2240.   "Symlink all marked files containing REGEXP to NEWNAME.
  2241. See function `dired-rename-regexp' for more info."
  2242.   t)
  2243.  
  2244. (autoload 'dired-upcase "dired-aux"
  2245.   "Rename all marked (or next ARG) files to upper case."
  2246.   t)
  2247.  
  2248. (autoload 'dired-downcase "dired-aux"
  2249.   "Rename all marked (or next ARG) files to lower case."
  2250.   t)
  2251.  
  2252. (autoload 'dired-maybe-insert-subdir "dired-aux"
  2253.   "Insert this subdirectory into the same dired buffer.
  2254. If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
  2255.   else inserts it at its natural place (as `ls -lR' would have done).
  2256. With a prefix arg, you may edit the ls switches used for this listing.
  2257.   You can add `R' to the switches to expand the whole tree starting at
  2258.   this subdirectory.
  2259. This function takes some pains to conform to `ls -lR' output."
  2260.   t)
  2261.  
  2262. (autoload 'dired-next-subdir "dired-aux"
  2263.   "Go to next subdirectory, regardless of level."
  2264.   t)
  2265.  
  2266. (autoload 'dired-prev-subdir "dired-aux"
  2267.   "Go to previous subdirectory, regardless of level.
  2268. When called interactively and not on a subdir line, go to this subdir's line."
  2269.   t)
  2270.  
  2271. (autoload 'dired-goto-subdir "dired-aux"
  2272.   "Go to end of header line of DIR in this dired buffer.
  2273. Return value of point on success, otherwise return nil.
  2274. The next char is either \\n, or \\r if DIR is hidden."
  2275.   t)
  2276.  
  2277. (autoload 'dired-mark-subdir-files "dired-aux"
  2278.   "Mark all files except `.' and `..'."
  2279.   t)
  2280.  
  2281. (autoload 'dired-kill-subdir "dired-aux"
  2282.   "Remove all lines of current subdirectory.
  2283. Lower levels are unaffected."
  2284.   t)
  2285.  
  2286. (autoload 'dired-tree-up "dired-aux"
  2287.   "Go up ARG levels in the dired tree."
  2288.   t)
  2289.  
  2290. (autoload 'dired-tree-down "dired-aux"
  2291.   "Go down in the dired tree."
  2292.   t)
  2293.  
  2294. (autoload 'dired-hide-subdir "dired-aux"
  2295.   "Hide or unhide the current subdirectory and move to next directory.
  2296. Optional prefix arg is a repeat factor.
  2297. Use \\[dired-hide-all] to (un)hide all directories."
  2298.   t)
  2299.  
  2300. (autoload 'dired-hide-all "dired-aux"
  2301.   "Hide all subdirectories, leaving only their header lines.
  2302. If there is already something hidden, make everything visible again.
  2303. Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
  2304.   t)
  2305.  
  2306. (if (eq system-type 'vax-vms)
  2307.     (load "dired-vms"))
  2308.  
  2309. (provide 'dired)
  2310.  
  2311. (run-hooks 'dired-load-hook)        ; for your customizations
  2312.  
  2313. ;;; dired.el ends here
  2314.